home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / binutils.7 / binutils / binutils-2.7 / ld / ldlang.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-04  |  90.8 KB  |  3,557 lines

  1. /* Linker command language support.
  2.    Copyright (C) 1991, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
  3.  
  4. This file is part of GLD, the Gnu Linker.
  5.  
  6. GLD is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GLD is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GLD; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  19.  
  20. #include "bfd.h"
  21. #include "sysdep.h"
  22. #include "libiberty.h"
  23. #include "bfdlink.h"
  24.  
  25. #include "ld.h"
  26. #include "ldmain.h"
  27. #include "ldgram.h"
  28. #include "ldexp.h"
  29. #include "ldlang.h"
  30. #include "ldemul.h"
  31. #include "ldlex.h"
  32. #include "ldmisc.h"
  33. #include "ldctor.h"
  34. #include "ldfile.h"
  35.  
  36. /* FORWARDS */
  37. static lang_statement_union_type *new_statement PARAMS ((enum statement_enum,
  38.                              size_t,
  39.                              lang_statement_list_type*));
  40.  
  41.  
  42. /* LOCALS */
  43. static struct obstack stat_obstack;
  44.  
  45. #define obstack_chunk_alloc xmalloc
  46. #define obstack_chunk_free free
  47. static CONST char *startup_file;
  48. static lang_statement_list_type input_file_chain;
  49. static boolean placed_commons = false;
  50. static lang_output_section_statement_type *default_common_section;
  51. static boolean map_option_f;
  52. static bfd_vma print_dot;
  53. static lang_input_statement_type *first_file;
  54. static lang_statement_list_type lang_output_section_statement;
  55. static CONST char *current_target;
  56. static CONST char *output_target;
  57. static lang_statement_list_type statement_list;
  58. static struct lang_phdr *lang_phdr_list;
  59.  
  60. static void lang_for_each_statement_worker
  61.   PARAMS ((void (*func) (lang_statement_union_type *),
  62.        lang_statement_union_type *s));
  63. static lang_input_statement_type *new_afile
  64.   PARAMS ((const char *name, lang_input_file_enum_type file_type,
  65.        const char *target, boolean add_to_list));
  66. static void init_os PARAMS ((lang_output_section_statement_type *s));
  67. static void exp_init_os PARAMS ((etree_type *));
  68. static void section_already_linked PARAMS ((bfd *, asection *, PTR));
  69. static void wild_section PARAMS ((lang_wild_statement_type *ptr,
  70.                   const char *section,
  71.                   lang_input_statement_type *file,
  72.                   lang_output_section_statement_type *output));
  73. static lang_input_statement_type *lookup_name PARAMS ((const char *name));
  74. static void load_symbols PARAMS ((lang_input_statement_type *entry,
  75.                   lang_statement_list_type *));
  76. static void wild PARAMS ((lang_wild_statement_type *s,
  77.               const char *section, const char *file,
  78.               const char *target,
  79.               lang_output_section_statement_type *output));
  80. static bfd *open_output PARAMS ((const char *name));
  81. static void ldlang_open_output PARAMS ((lang_statement_union_type *statement));
  82. static void open_input_bfds
  83.   PARAMS ((lang_statement_union_type *statement, boolean));
  84. static void lang_reasonable_defaults PARAMS ((void));
  85. static void lang_place_undefineds PARAMS ((void));
  86. static void map_input_to_output_sections
  87.   PARAMS ((lang_statement_union_type *s,
  88.        const char *target,
  89.        lang_output_section_statement_type *output_section_statement));
  90. static void print_output_section_statement
  91.   PARAMS ((lang_output_section_statement_type *output_section_statement));
  92. static void print_assignment
  93.   PARAMS ((lang_assignment_statement_type *assignment,
  94.        lang_output_section_statement_type *output_section));
  95. static void print_input_statement PARAMS ((lang_input_statement_type *statm));
  96. static void print_input_section PARAMS ((lang_input_section_type *in));
  97. static void print_fill_statement PARAMS ((lang_fill_statement_type *fill));
  98. static void print_data_statement PARAMS ((lang_data_statement_type *data));
  99. static void print_address_statement PARAMS ((lang_address_statement_type *));
  100. static void print_reloc_statement PARAMS ((lang_reloc_statement_type *reloc));
  101. static void print_padding_statement PARAMS ((lang_padding_statement_type *s));
  102. static void print_wild_statement
  103.   PARAMS ((lang_wild_statement_type *w,
  104.        lang_output_section_statement_type *os));
  105. static void print_group
  106.   PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
  107. static void print_statement PARAMS ((lang_statement_union_type *s,
  108.                      lang_output_section_statement_type *os));
  109. static void print_statement_list PARAMS ((lang_statement_union_type *s,
  110.                       lang_output_section_statement_type *os));
  111. static void print_statements PARAMS ((void));
  112. static bfd_vma insert_pad PARAMS ((lang_statement_union_type **this_ptr,
  113.                    fill_type fill, unsigned int power,
  114.                    asection *output_section_statement,
  115.                    bfd_vma dot));
  116. static bfd_vma size_input_section
  117.   PARAMS ((lang_statement_union_type **this_ptr,
  118.        lang_output_section_statement_type *output_section_statement,
  119.        fill_type fill, bfd_vma dot, boolean relax));
  120. static void lang_finish PARAMS ((void));
  121. static void lang_check PARAMS ((void));
  122. static void lang_common PARAMS ((void));
  123. static boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
  124. static void lang_place_orphans PARAMS ((void));
  125. static int topower PARAMS ((int));
  126. static void lang_set_startof PARAMS ((void));
  127. static void reset_memory_regions PARAMS ((void));
  128. static void lang_record_phdrs PARAMS ((void));
  129.  
  130. /* EXPORTS */
  131. lang_output_section_statement_type *abs_output_section;
  132. lang_statement_list_type *stat_ptr = &statement_list;
  133. lang_statement_list_type file_chain = { 0 };
  134. const char *entry_symbol = NULL;
  135. boolean entry_from_cmdline;
  136. boolean lang_has_input_file = false;
  137. boolean had_output_filename = false;
  138. boolean lang_float_flag = false;
  139. boolean delete_output_file_on_failure = false;
  140.  
  141. etree_type *base; /* Relocation base - or null */
  142.  
  143.  
  144. #if defined(__STDC__) || defined(ALMOST_STDC)
  145. #define cat(a,b) a##b
  146. #else
  147. #define cat(a,b) a/**/b
  148. #endif
  149.  
  150. #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
  151.  
  152. #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
  153.  
  154. #define outside_symbol_address(q) ((q)->value +   outside_section_address(q->section))
  155.  
  156. #define SECTION_NAME_MAP_LENGTH (16)
  157.  
  158. PTR
  159. stat_alloc (size)
  160.      size_t size;
  161. {
  162.   return obstack_alloc (&stat_obstack, size);
  163. }
  164.  
  165. /*----------------------------------------------------------------------
  166.   lang_for_each_statement walks the parse tree and calls the provided
  167.   function for each node
  168. */
  169.  
  170. static void
  171. lang_for_each_statement_worker (func, s)
  172.      void (*func) PARAMS ((lang_statement_union_type *));
  173.      lang_statement_union_type *s;
  174. {
  175.   for (; s != (lang_statement_union_type *) NULL; s = s->next)
  176.     {
  177.       func (s);
  178.  
  179.       switch (s->header.type)
  180.     {
  181.     case lang_constructors_statement_enum:
  182.       lang_for_each_statement_worker (func, constructor_list.head);
  183.       break;
  184.     case lang_output_section_statement_enum:
  185.       lang_for_each_statement_worker
  186.         (func,
  187.          s->output_section_statement.children.head);
  188.       break;
  189.     case lang_wild_statement_enum:
  190.       lang_for_each_statement_worker
  191.         (func,
  192.          s->wild_statement.children.head);
  193.       break;
  194.     case lang_group_statement_enum:
  195.       lang_for_each_statement_worker (func,
  196.                       s->group_statement.children.head);
  197.       break;
  198.     case lang_data_statement_enum:
  199.     case lang_reloc_statement_enum:
  200.     case lang_object_symbols_statement_enum:
  201.     case lang_output_statement_enum:
  202.     case lang_target_statement_enum:
  203.     case lang_input_section_enum:
  204.     case lang_input_statement_enum:
  205.     case lang_assignment_statement_enum:
  206.     case lang_padding_statement_enum:
  207.     case lang_address_statement_enum:
  208.     case lang_fill_statement_enum:
  209.       break;
  210.     default:
  211.       FAIL ();
  212.       break;
  213.     }
  214.     }
  215. }
  216.  
  217. void
  218. lang_for_each_statement (func)
  219.      void (*func) PARAMS ((lang_statement_union_type *));
  220. {
  221.   lang_for_each_statement_worker (func,
  222.                   statement_list.head);
  223. }
  224.  
  225. /*----------------------------------------------------------------------*/
  226. void
  227. lang_list_init (list)
  228.      lang_statement_list_type *list;
  229. {
  230.   list->head = (lang_statement_union_type *) NULL;
  231.   list->tail = &list->head;
  232. }
  233.  
  234. /*----------------------------------------------------------------------
  235.  
  236.   build a new statement node for the parse tree
  237.  
  238.  */
  239.  
  240. static
  241. lang_statement_union_type *
  242. new_statement (type, size, list)
  243.      enum statement_enum type;
  244.      size_t size;
  245.      lang_statement_list_type * list;
  246. {
  247.   lang_statement_union_type *new = (lang_statement_union_type *)
  248.   stat_alloc (size);
  249.  
  250.   new->header.type = type;
  251.   new->header.next = (lang_statement_union_type *) NULL;
  252.   lang_statement_append (list, new, &new->header.next);
  253.   return new;
  254. }
  255.  
  256. /*
  257.   Build a new input file node for the language. There are several ways
  258.   in which we treat an input file, eg, we only look at symbols, or
  259.   prefix it with a -l etc.
  260.  
  261.   We can be supplied with requests for input files more than once;
  262.   they may, for example be split over serveral lines like foo.o(.text)
  263.   foo.o(.data) etc, so when asked for a file we check that we havn't
  264.   got it already so we don't duplicate the bfd.
  265.  
  266.  */
  267. static lang_input_statement_type *
  268. new_afile (name, file_type, target, add_to_list)
  269.      CONST char *name;
  270.      lang_input_file_enum_type file_type;
  271.      CONST char *target;
  272.      boolean add_to_list;
  273. {
  274.   lang_input_statement_type *p;
  275.  
  276.   if (add_to_list)
  277.     p = new_stat (lang_input_statement, stat_ptr);
  278.   else
  279.     {
  280.       p = ((lang_input_statement_type *)
  281.        stat_alloc (sizeof (lang_input_statement_type)));
  282.       p->header.next = NULL;
  283.     }
  284.  
  285.   lang_has_input_file = true;
  286.   p->target = target;
  287.   switch (file_type)
  288.     {
  289.     case lang_input_file_is_symbols_only_enum:
  290.       p->filename = name;
  291.       p->is_archive = false;
  292.       p->real = true;
  293.       p->local_sym_name = name;
  294.       p->just_syms_flag = true;
  295.       p->search_dirs_flag = false;
  296.       break;
  297.     case lang_input_file_is_fake_enum:
  298.       p->filename = name;
  299.       p->is_archive = false;
  300.       p->real = false;
  301.       p->local_sym_name = name;
  302.       p->just_syms_flag = false;
  303.       p->search_dirs_flag = false;
  304.       break;
  305.     case lang_input_file_is_l_enum:
  306.       p->is_archive = true;
  307.       p->filename = name;
  308.       p->real = true;
  309.       p->local_sym_name = concat ("-l", name, (const char *) NULL);
  310.       p->just_syms_flag = false;
  311.       p->search_dirs_flag = true;
  312.       break;
  313.     case lang_input_file_is_marker_enum:
  314.       p->filename = name;
  315.       p->is_archive = false;
  316.       p->real = false;
  317.       p->local_sym_name = name;
  318.       p->just_syms_flag = false;
  319.       p->search_dirs_flag = true;
  320.       break;
  321.     case lang_input_file_is_search_file_enum:
  322.       p->filename = name;
  323.       p->is_archive = false;
  324.       p->real = true;
  325.       p->local_sym_name = name;
  326.       p->just_syms_flag = false;
  327.       p->search_dirs_flag = true;
  328.       break;
  329.     case lang_input_file_is_file_enum:
  330.       p->filename = name;
  331.       p->is_archive = false;
  332.       p->real = true;
  333.       p->local_sym_name = name;
  334.       p->just_syms_flag = false;
  335.       p->search_dirs_flag = false;
  336.       break;
  337.     default:
  338.       FAIL ();
  339.     }
  340.   p->the_bfd = (bfd *) NULL;
  341.   p->asymbols = (asymbol **) NULL;
  342.   p->next_real_file = (lang_statement_union_type *) NULL;
  343.   p->next = (lang_statement_union_type *) NULL;
  344.   p->symbol_count = 0;
  345.   p->dynamic = config.dynamic_link;
  346.   p->whole_archive = whole_archive;
  347.   p->loaded = false;
  348.   lang_statement_append (&input_file_chain,
  349.              (lang_statement_union_type *) p,
  350.              &p->next_real_file);
  351.   return p;
  352. }
  353.  
  354. lang_input_statement_type *
  355. lang_add_input_file (name, file_type, target)
  356.      CONST char *name;
  357.      lang_input_file_enum_type file_type;
  358.      CONST char *target;
  359. {
  360.   lang_has_input_file = true;
  361.   return new_afile (name, file_type, target, true);
  362. }
  363.  
  364. /* Build enough state so that the parser can build its tree */
  365. void
  366. lang_init ()
  367. {
  368.   obstack_begin (&stat_obstack, 1000);
  369.  
  370.   stat_ptr = &statement_list;
  371.  
  372.   lang_list_init (stat_ptr);
  373.  
  374.   lang_list_init (&input_file_chain);
  375.   lang_list_init (&lang_output_section_statement);
  376.   lang_list_init (&file_chain);
  377.   first_file = lang_add_input_file ((char *) NULL,
  378.                     lang_input_file_is_marker_enum,
  379.                     (char *) NULL);
  380.   abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
  381.  
  382.   abs_output_section->bfd_section = bfd_abs_section_ptr;
  383.  
  384. }
  385.  
  386. /*----------------------------------------------------------------------
  387.  A region is an area of memory declared with the
  388.  MEMORY {  name:org=exp, len=exp ... }
  389.  syntax.
  390.  
  391.  We maintain a list of all the regions here
  392.  
  393.  If no regions are specified in the script, then the default is used
  394.  which is created when looked up to be the entire data space
  395. */
  396.  
  397. static lang_memory_region_type *lang_memory_region_list;
  398. static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
  399.  
  400. lang_memory_region_type *
  401. lang_memory_region_lookup (name)
  402.      CONST char *CONST name;
  403. {
  404.  
  405.   lang_memory_region_type *p = lang_memory_region_list;
  406.  
  407.   for (p = lang_memory_region_list;
  408.        p != (lang_memory_region_type *) NULL;
  409.        p = p->next)
  410.     {
  411.       if (strcmp (p->name, name) == 0)
  412.     {
  413.       return p;
  414.     }
  415.     }
  416.  
  417. #if 0
  418.   /* This code used to always use the first region in the list as the
  419.      default region.  I changed it to instead use a region
  420.      encompassing all of memory as the default region.  This permits
  421.      NOLOAD sections to work reasonably without requiring a region.
  422.      People should specify what region they mean, if they really want
  423.      a region.  */
  424.   if (strcmp (name, "*default*") == 0)
  425.     {
  426.       if (lang_memory_region_list != (lang_memory_region_type *) NULL)
  427.     {
  428.       return lang_memory_region_list;
  429.     }
  430.     }
  431. #endif
  432.  
  433.   {
  434.     lang_memory_region_type *new =
  435.     (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
  436.  
  437.     new->name = buystring (name);
  438.     new->next = (lang_memory_region_type *) NULL;
  439.  
  440.     *lang_memory_region_list_tail = new;
  441.     lang_memory_region_list_tail = &new->next;
  442.     new->origin = 0;
  443.     new->length = ~(bfd_size_type)0;
  444.     new->current = 0;
  445.     new->had_full_message = false;
  446.  
  447.     return new;
  448.   }
  449. }
  450.  
  451.  
  452. lang_output_section_statement_type *
  453. lang_output_section_find (name)
  454.      CONST char *CONST name;
  455. {
  456.   lang_statement_union_type *u;
  457.   lang_output_section_statement_type *lookup;
  458.  
  459.   for (u = lang_output_section_statement.head;
  460.        u != (lang_statement_union_type *) NULL;
  461.        u = lookup->next)
  462.     {
  463.       lookup = &u->output_section_statement;
  464.       if (strcmp (name, lookup->name) == 0)
  465.     {
  466.       return lookup;
  467.     }
  468.     }
  469.   return (lang_output_section_statement_type *) NULL;
  470. }
  471.  
  472. lang_output_section_statement_type *
  473. lang_output_section_statement_lookup (name)
  474.      CONST char *CONST name;
  475. {
  476.   lang_output_section_statement_type *lookup;
  477.  
  478.   lookup = lang_output_section_find (name);
  479.   if (lookup == (lang_output_section_statement_type *) NULL)
  480.     {
  481.  
  482.       lookup = (lang_output_section_statement_type *)
  483.     new_stat (lang_output_section_statement, stat_ptr);
  484.       lookup->region = (lang_memory_region_type *) NULL;
  485.       lookup->fill = 0;
  486.       lookup->block_value = 1;
  487.       lookup->name = name;
  488.  
  489.       lookup->next = (lang_statement_union_type *) NULL;
  490.       lookup->bfd_section = (asection *) NULL;
  491.       lookup->processed = false;
  492.       lookup->sectype = normal_section;
  493.       lookup->addr_tree = (etree_type *) NULL;
  494.       lang_list_init (&lookup->children);
  495.  
  496.       lookup->memspec = (CONST char *) NULL;
  497.       lookup->flags = 0;
  498.       lookup->subsection_alignment = -1;
  499.       lookup->section_alignment = -1;
  500.       lookup->load_base = (union etree_union *) NULL;
  501.       lookup->phdrs = NULL;
  502.  
  503.       lang_statement_append (&lang_output_section_statement,
  504.                  (lang_statement_union_type *) lookup,
  505.                  &lookup->next);
  506.     }
  507.   return lookup;
  508. }
  509.  
  510. void
  511. lang_map ()
  512. {
  513.   lang_memory_region_type *m;
  514.  
  515.   minfo ("\nMemory Configuration\n\n");
  516.   fprintf (config.map_file, "%-16s %-18s %-18s\n",
  517.        "Name", "Origin", "Length");
  518.  
  519.   for (m = lang_memory_region_list;
  520.        m != (lang_memory_region_type *) NULL;
  521.        m = m->next)
  522.     {
  523.       char buf[100];
  524.       int len;
  525.  
  526.       fprintf (config.map_file, "%-16s ", m->name);
  527.  
  528.       sprintf_vma (buf, m->origin);
  529.       minfo ("0x%s ", buf);
  530.       len = strlen (buf);
  531.       while (len < 16)
  532.     {
  533.       print_space ();
  534.       ++len;
  535.     }
  536.  
  537.       minfo ("0x%V\n", m->length);
  538.     }
  539.  
  540.   fprintf (config.map_file, "\nLinker script and memory map\n\n");
  541.  
  542.   print_statements ();
  543. }
  544.  
  545. /* Initialize an output section.  */
  546.  
  547. static void
  548. init_os (s)
  549.      lang_output_section_statement_type *s;
  550. {
  551.   section_userdata_type *new;
  552.  
  553.   if (s->bfd_section != NULL)
  554.     return;
  555.  
  556.   if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
  557.     einfo ("%P%F: Illegal use of `%s' section", DISCARD_SECTION_NAME);
  558.  
  559.   new = ((section_userdata_type *)
  560.      stat_alloc (sizeof (section_userdata_type)));
  561.  
  562.   s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
  563.   if (s->bfd_section == (asection *) NULL)
  564.     s->bfd_section = bfd_make_section (output_bfd, s->name);
  565.   if (s->bfd_section == (asection *) NULL)
  566.     {
  567.       einfo ("%P%F: output format %s cannot represent section called %s\n",
  568.          output_bfd->xvec->name, s->name);
  569.     }
  570.   s->bfd_section->output_section = s->bfd_section;
  571.  
  572.   /* We initialize an output sections output offset to minus its own */
  573.   /* vma to allow us to output a section through itself */
  574.   s->bfd_section->output_offset = 0;
  575.   get_userdata (s->bfd_section) = (PTR) new;
  576.  
  577.   /* If there is a base address, make sure that any sections it might
  578.      mention are initialized.  */
  579.   if (s->addr_tree != NULL)
  580.     exp_init_os (s->addr_tree);
  581. }
  582.  
  583. /* Make sure that all output sections mentioned in an expression are
  584.    initialized.  */
  585.  
  586. static void
  587. exp_init_os (exp)
  588.      etree_type *exp;
  589. {
  590.   switch (exp->type.node_class)
  591.     {
  592.     case etree_assign:
  593.       exp_init_os (exp->assign.src);
  594.       break;
  595.  
  596.     case etree_binary:
  597.       exp_init_os (exp->binary.lhs);
  598.       exp_init_os (exp->binary.rhs);
  599.       break;
  600.  
  601.     case etree_trinary:
  602.       exp_init_os (exp->trinary.cond);
  603.       exp_init_os (exp->trinary.lhs);
  604.       exp_init_os (exp->trinary.rhs);
  605.       break;
  606.  
  607.     case etree_unary:
  608.       exp_init_os (exp->unary.child);
  609.       break;
  610.  
  611.     case etree_name:
  612.       switch (exp->type.node_code)
  613.     {
  614.     case ADDR:
  615.     case SIZEOF:
  616.       {
  617.         lang_output_section_statement_type *os;
  618.  
  619.         os = lang_output_section_find (exp->name.name);
  620.         if (os != NULL && os->bfd_section == NULL)
  621.           init_os (os);
  622.       }
  623.     }
  624.       break;
  625.  
  626.     default:
  627.       break;
  628.     }
  629. }
  630.  
  631. /* Sections marked with the SEC_LINK_ONCE flag should only be linked
  632.    once into the output.  This routine checks each sections, and
  633.    arranges to discard it if a section of the same name has already
  634.    been linked.  This code assumes that all relevant sections have the
  635.    SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
  636.    section name.  This is called via bfd_map_over_sections.  */
  637.  
  638. /*ARGSUSED*/
  639. static void
  640. section_already_linked (abfd, sec, ignore)
  641.      bfd *abfd;
  642.      asection *sec;
  643.      PTR ignore;
  644. {
  645.   struct sec_link_once
  646.     {
  647.       struct sec_link_once *next;
  648.       asection *sec;
  649.     };
  650.   static struct sec_link_once *sec_link_once_list;
  651.   flagword flags;
  652.   const char *name;
  653.   struct sec_link_once *l;
  654.  
  655.   flags = bfd_get_section_flags (abfd, sec);
  656.  
  657.   if ((flags & SEC_LINK_ONCE) == 0)
  658.     return;
  659.  
  660.   name = bfd_get_section_name (abfd, sec);
  661.  
  662.   for (l = sec_link_once_list; l != NULL; l = l->next)
  663.     {
  664.       if (strcmp (name, bfd_get_section_name (l->sec->owner, l->sec)) == 0)
  665.     {
  666.       /* The section has already been linked.  See if we should
  667.              issue a warning.  */
  668.       switch (flags & SEC_LINK_DUPLICATES)
  669.         {
  670.         default:
  671.           abort ();
  672.  
  673.         case SEC_LINK_DUPLICATES_DISCARD:
  674.           break;
  675.  
  676.         case SEC_LINK_DUPLICATES_ONE_ONLY:
  677.           einfo ("%P: %B: warning: ignoring duplicate section `%s'\n",
  678.              abfd, name);
  679.           break;
  680.  
  681.         case SEC_LINK_DUPLICATES_SAME_CONTENTS:
  682.           /* FIXME: We should really dig out the contents of both
  683.                  sections and memcmp them.  The COFF/PE spec says that
  684.                  the Microsoft linker does not implement this
  685.                  correctly, so I'm not going to bother doing it
  686.                  either.  */
  687.           /* Fall through.  */
  688.         case SEC_LINK_DUPLICATES_SAME_SIZE:
  689.           if (bfd_section_size (abfd, sec)
  690.           != bfd_section_size (l->sec->owner, l->sec))
  691.         einfo ("%P: %B: warning: duplicate section `%s' has different size\n",
  692.                abfd, name);
  693.           break;
  694.         }
  695.  
  696.       /* Set the output_section field so that wild_doit does not
  697.          create a lang_input_section structure for this section.  */
  698.       sec->output_section = bfd_abs_section_ptr;
  699.  
  700.       return;
  701.     }
  702.     }
  703.  
  704.   /* This is the first section with this name.  Record it.  */
  705.  
  706.   l = (struct sec_link_once *) xmalloc (sizeof *l);
  707.   l->sec = sec;
  708.   l->next = sec_link_once_list;
  709.   sec_link_once_list = l;
  710. }
  711.  
  712. /* The wild routines.
  713.  
  714.    These expand statements like *(.text) and foo.o to a list of
  715.    explicit actions, like foo.o(.text), bar.o(.text) and
  716.    foo.o(.text, .data).  */
  717.  
  718. /* Add SECTION to the output section OUTPUT.  Do this by creating a
  719.    lang_input_section statement which is placed at PTR.  FILE is the
  720.    input file which holds SECTION.  */
  721.  
  722. void
  723. wild_doit (ptr, section, output, file)
  724.      lang_statement_list_type *ptr;
  725.      asection *section;
  726.      lang_output_section_statement_type *output;
  727.      lang_input_statement_type *file;
  728. {
  729.   flagword flags;
  730.   boolean discard;
  731.  
  732.   flags = bfd_get_section_flags (section->owner, section);
  733.  
  734.   discard = false;
  735.  
  736.   /* If we are doing a final link, discard sections marked with
  737.      SEC_EXCLUDE.  */
  738.   if (! link_info.relocateable
  739.       && (flags & SEC_EXCLUDE) != 0)
  740.     discard = true;
  741.  
  742.   /* Discard input sections which are assigned to a section named
  743.      DISCARD_SECTION_NAME.  */
  744.   if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
  745.     discard = true;
  746.  
  747.   /* Discard debugging sections if we are stripping debugging
  748.      information.  */
  749.   if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
  750.       && (flags & SEC_DEBUGGING) != 0)
  751.     discard = true;
  752.  
  753.   if (discard)
  754.     {
  755.       if (section->output_section == NULL)
  756.     {
  757.       /* This prevents future calls from assigning this section.  */
  758.       section->output_section = bfd_abs_section_ptr;
  759.     }
  760.       return;
  761.     }
  762.  
  763.   if (section->output_section == NULL)
  764.     {
  765.       lang_input_section_type *new;
  766.  
  767.       if (output->bfd_section == NULL)
  768.     init_os (output);
  769.  
  770.       /* Add a section reference to the list */
  771.       new = new_stat (lang_input_section, ptr);
  772.  
  773.       new->section = section;
  774.       new->ifile = file;
  775.       section->output_section = output->bfd_section;
  776.  
  777.       /* We don't copy the SEC_NEVER_LOAD flag from an input section
  778.      to an output section, because we want to be able to include a
  779.      SEC_NEVER_LOAD section in the middle of an otherwise loaded
  780.      section (I don't know why we want to do this, but we do).
  781.      build_link_order in ldwrite.c handles this case by turning
  782.      the embedded SEC_NEVER_LOAD section into a fill.
  783.  
  784.      If final link, don't copy the SEC_LINK_ONCE flags, they've already
  785.      been processed.  One reason to do this is that on pe format targets,
  786.      .text$foo sections go into .text and it's odd to see .text with
  787.      SEC_LINK_ONCE set.  */
  788.  
  789.       section->output_section->flags |=
  790.     section->flags & (flagword) (~ (SEC_NEVER_LOAD
  791.                     | (! link_info.relocateable
  792.                        ? SEC_LINK_ONCE | SEC_LINK_DUPLICATES
  793.                        : 0)));
  794.  
  795.       switch (output->sectype)
  796.     {
  797.     case normal_section:
  798.       break;
  799.     case dsect_section:
  800.     case copy_section:
  801.     case info_section:
  802.     case overlay_section:
  803.       output->bfd_section->flags &= ~SEC_ALLOC;
  804.       break;
  805.     case noload_section:
  806.       output->bfd_section->flags &= ~SEC_LOAD;
  807.       output->bfd_section->flags |= SEC_NEVER_LOAD;
  808.       break;
  809.     }
  810.  
  811.       if (section->alignment_power > output->bfd_section->alignment_power)
  812.     output->bfd_section->alignment_power = section->alignment_power;
  813.  
  814.       /* If supplied an aligment, then force it.  */
  815.       if (output->section_alignment != -1)
  816.     output->bfd_section->alignment_power = output->section_alignment;
  817.     }
  818. }
  819.  
  820. /* Expand a wild statement for a particular FILE.  SECTION may be
  821.    NULL, in which case it is a wild card.  */
  822.  
  823. static void
  824. wild_section (ptr, section, file, output)
  825.      lang_wild_statement_type *ptr;
  826.      const char *section;
  827.      lang_input_statement_type *file;
  828.      lang_output_section_statement_type *output;
  829. {
  830.   if (file->just_syms_flag == false)
  831.     {
  832.       register asection *s;
  833.  
  834.       for (s = file->the_bfd->sections; s != NULL; s = s->next)
  835.     {
  836.       /* Attach all sections named SECTION.  If SECTION is NULL,
  837.          then attach all sections.
  838.  
  839.          Previously, if SECTION was NULL, this code did not call
  840.          wild_doit if the SEC_IS_COMMON flag was set for the
  841.          section.  I did not understand that, and I took it out.
  842.          --ian@cygnus.com.  */
  843.  
  844.       if (section == NULL
  845.           || strcmp (bfd_get_section_name (file->the_bfd, s),
  846.              section) == 0)
  847.         wild_doit (&ptr->children, s, output, file);
  848.     }
  849.     }
  850. }
  851.  
  852. /* This is passed a file name which must have been seen already and
  853.    added to the statement tree.  We will see if it has been opened
  854.    already and had its symbols read.  If not then we'll read it.  */
  855.  
  856. static lang_input_statement_type *
  857. lookup_name (name)
  858.      const char *name;
  859. {
  860.   lang_input_statement_type *search;
  861.  
  862.   for (search = (lang_input_statement_type *) input_file_chain.head;
  863.        search != (lang_input_statement_type *) NULL;
  864.        search = (lang_input_statement_type *) search->next_real_file)
  865.     {
  866.       if (search->filename == (char *) NULL && name == (char *) NULL)
  867.     return search;
  868.       if (search->filename != (char *) NULL
  869.       && name != (char *) NULL
  870.       && strcmp (search->filename, name) == 0)
  871.     break;
  872.     }
  873.  
  874.   if (search == (lang_input_statement_type *) NULL)
  875.     search = new_afile (name, lang_input_file_is_file_enum, default_target,
  876.             false);
  877.  
  878.   /* If we have already added this file, or this file is not real
  879.      (FIXME: can that ever actually happen?) or the name is NULL
  880.      (FIXME: can that ever actually happen?) don't add this file.  */
  881.   if (search->loaded
  882.       || ! search->real
  883.       || search->filename == (const char *) NULL)
  884.     return search;
  885.  
  886.   load_symbols (search, (lang_statement_list_type *) NULL);
  887.  
  888.   return search;
  889. }
  890.  
  891. /* Get the symbols for an input file.  */
  892.  
  893. static void
  894. load_symbols (entry, place)
  895.      lang_input_statement_type *entry;
  896.      lang_statement_list_type *place;
  897. {
  898.   char **matching;
  899.  
  900.   if (entry->loaded)
  901.     return;
  902.  
  903.   ldfile_open_file (entry);
  904.  
  905.   if (! bfd_check_format (entry->the_bfd, bfd_archive)
  906.       && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
  907.     {
  908.       bfd_error_type err;
  909.       lang_statement_list_type *hold;
  910.  
  911.       err = bfd_get_error ();
  912.       if (err == bfd_error_file_ambiguously_recognized)
  913.     {
  914.       char **p;
  915.  
  916.       einfo ("%B: file not recognized: %E\n", entry->the_bfd);
  917.       einfo ("%B: matching formats:", entry->the_bfd);
  918.       for (p = matching; *p != NULL; p++)
  919.         einfo (" %s", *p);
  920.       einfo ("%F\n");
  921.     }
  922.       else if (err != bfd_error_file_not_recognized
  923.            || place == NULL)
  924.     einfo ("%F%B: file not recognized: %E\n", entry->the_bfd);
  925.  
  926.       bfd_close (entry->the_bfd);
  927.       entry->the_bfd = NULL;
  928.  
  929.       /* See if the emulation has some special knowledge.  */
  930.  
  931.       if (ldemul_unrecognized_file (entry))
  932.     return;
  933.  
  934.       /* Try to interpret the file as a linker script.  */
  935.  
  936.       ldfile_open_command_file (entry->filename);
  937.  
  938.       hold = stat_ptr;
  939.       stat_ptr = place;
  940.  
  941.       ldfile_assumed_script = true;
  942.       parser_input = input_script;
  943.       yyparse ();
  944.       ldfile_assumed_script = false;
  945.  
  946.       stat_ptr = hold;
  947.  
  948.       return;
  949.     }
  950.  
  951.   /* We don't call ldlang_add_file for an archive.  Instead, the
  952.      add_symbols entry point will call ldlang_add_file, via the
  953.      add_archive_element callback, for each element of the archive
  954.      which is used.  */
  955.   switch (bfd_get_format (entry->the_bfd))
  956.     {
  957.     default:
  958.       break;
  959.  
  960.     case bfd_object:
  961.       ldlang_add_file (entry);
  962.       if (trace_files || trace_file_tries)
  963.     info_msg ("%I\n", entry);
  964.       break;
  965.  
  966.     case bfd_archive:
  967.       if (entry->whole_archive)
  968.     {
  969.       bfd *member = bfd_openr_next_archived_file (entry->the_bfd,
  970.                               (bfd *) NULL);
  971.       while (member != NULL)
  972.         {
  973.           if (! bfd_check_format (member, bfd_object))
  974.         einfo ("%F%B: object %B in archive is not object\n",
  975.                entry->the_bfd, member);
  976.           if (! ((*link_info.callbacks->add_archive_element)
  977.              (&link_info, member, "--whole-archive")))
  978.         abort ();
  979.           if (! bfd_link_add_symbols (member, &link_info))
  980.         einfo ("%F%B: could not read symbols: %E\n", member);
  981.           member = bfd_openr_next_archived_file (entry->the_bfd,
  982.                              member);
  983.         }
  984.  
  985.       entry->loaded = true;
  986.  
  987.       return;
  988.     }
  989.     }
  990.  
  991.   if (! bfd_link_add_symbols (entry->the_bfd, &link_info))
  992.     einfo ("%F%B: could not read symbols: %E\n", entry->the_bfd);
  993.  
  994.   entry->loaded = true;
  995. }
  996.  
  997. /* Handle a wild statement.  SECTION or FILE or both may be NULL,
  998.    indicating that it is a wildcard.  Separate lang_input_section
  999.    statements are created for each part of the expansion; they are
  1000.    added after the wild statement S.  OUTPUT is the output section.  */
  1001.  
  1002. static void
  1003. wild (s, section, file, target, output)
  1004.      lang_wild_statement_type *s;
  1005.      const char *section;
  1006.      const char *file;
  1007.      const char *target;
  1008.      lang_output_section_statement_type *output;
  1009. {
  1010.   lang_input_statement_type *f;
  1011.  
  1012.   if (file == (char *) NULL)
  1013.     {
  1014.       /* Perform the iteration over all files in the list */
  1015.       for (f = (lang_input_statement_type *) file_chain.head;
  1016.        f != (lang_input_statement_type *) NULL;
  1017.        f = (lang_input_statement_type *) f->next)
  1018.     {
  1019.       wild_section (s, section, f, output);
  1020.     }
  1021.     }
  1022.   else
  1023.     {
  1024.       /* Perform the iteration over a single file */
  1025.       f = lookup_name (file);
  1026.       if (f->the_bfd == NULL
  1027.       || ! bfd_check_format (f->the_bfd, bfd_archive))
  1028.     wild_section (s, section, f, output);
  1029.       else
  1030.     {
  1031.       bfd *member;
  1032.  
  1033.       /* This is an archive file.  We must map each member of the
  1034.              archive separately.  */
  1035.       member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
  1036.       while (member != NULL)
  1037.         {
  1038.           /* When lookup_name is called, it will call the
  1039.                  add_symbols entry point for the archive.  For each
  1040.                  element of the archive which is included, BFD will
  1041.                  call ldlang_add_file, which will set the usrdata
  1042.                  field of the member to the lang_input_statement.  */
  1043.           if (member->usrdata != NULL)
  1044.         {
  1045.           wild_section (s, section,
  1046.                 (lang_input_statement_type *) member->usrdata,
  1047.                 output);
  1048.         }
  1049.  
  1050.           member = bfd_openr_next_archived_file (f->the_bfd, member);
  1051.         }
  1052.     }
  1053.     }
  1054.  
  1055.   if (section != (char *) NULL
  1056.       && strcmp (section, "COMMON") == 0
  1057.       && default_common_section == NULL)
  1058.     {
  1059.       /* Remember the section that common is going to in case we later
  1060.          get something which doesn't know where to put it.  */
  1061.       default_common_section = output;
  1062.     }
  1063. }
  1064.  
  1065. /* Open the output file.  */
  1066.  
  1067. static bfd *
  1068. open_output (name)
  1069.      const char *name;
  1070. {
  1071.   bfd *output;
  1072.  
  1073.   if (output_target == (char *) NULL)
  1074.     {
  1075.       if (current_target != (char *) NULL)
  1076.     output_target = current_target;
  1077.       else
  1078.     output_target = default_target;
  1079.     }
  1080.   output = bfd_openw (name, output_target);
  1081.  
  1082.   if (output == (bfd *) NULL)
  1083.     {
  1084.       if (bfd_get_error () == bfd_error_invalid_target)
  1085.     {
  1086.       einfo ("%P%F: target %s not found\n", output_target);
  1087.     }
  1088.       einfo ("%P%F: cannot open output file %s: %E\n", name);
  1089.     }
  1090.  
  1091.   delete_output_file_on_failure = true;
  1092.  
  1093.   /*  output->flags |= D_PAGED;*/
  1094.  
  1095.   if (! bfd_set_format (output, bfd_object))
  1096.     einfo ("%P%F:%s: can not make object file: %E\n", name);
  1097.   if (! bfd_set_arch_mach (output,
  1098.                ldfile_output_architecture,
  1099.                ldfile_output_machine))
  1100.     einfo ("%P%F:%s: can not set architecture: %E\n", name);
  1101.  
  1102.   link_info.hash = bfd_link_hash_table_create (output);
  1103.   if (link_info.hash == (struct bfd_link_hash_table *) NULL)
  1104.     einfo ("%P%F: can not create link hash table: %E\n");
  1105.  
  1106.   bfd_set_gp_size (output, g_switch_value);
  1107.   return output;
  1108. }
  1109.  
  1110.  
  1111.  
  1112.  
  1113. static void
  1114. ldlang_open_output (statement)
  1115.      lang_statement_union_type * statement;
  1116. {
  1117.   switch (statement->header.type)
  1118.     {
  1119.     case lang_output_statement_enum:
  1120.       ASSERT (output_bfd == (bfd *) NULL);
  1121.       output_bfd = open_output (statement->output_statement.name);
  1122.       ldemul_set_output_arch ();
  1123.       if (config.magic_demand_paged && !link_info.relocateable)
  1124.     output_bfd->flags |= D_PAGED;
  1125.       else
  1126.     output_bfd->flags &= ~D_PAGED;
  1127.       if (config.text_read_only)
  1128.     output_bfd->flags |= WP_TEXT;
  1129.       else
  1130.     output_bfd->flags &= ~WP_TEXT;
  1131.       if (link_info.traditional_format)
  1132.     output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
  1133.       else
  1134.     output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
  1135.       break;
  1136.  
  1137.     case lang_target_statement_enum:
  1138.       current_target = statement->target_statement.target;
  1139.       break;
  1140.     default:
  1141.       break;
  1142.     }
  1143. }
  1144.  
  1145. /* Open all the input files.  */
  1146.  
  1147. static void
  1148. open_input_bfds (s, force)
  1149.      lang_statement_union_type *s;
  1150.      boolean force;
  1151. {
  1152.   for (; s != (lang_statement_union_type *) NULL; s = s->next)
  1153.     {
  1154.       switch (s->header.type)
  1155.     {
  1156.     case lang_constructors_statement_enum:
  1157.       open_input_bfds (constructor_list.head, force);
  1158.       break;
  1159.     case lang_output_section_statement_enum:
  1160.       open_input_bfds (s->output_section_statement.children.head, force);
  1161.       break;
  1162.     case lang_wild_statement_enum:
  1163.       /* Maybe we should load the file's symbols */
  1164.       if (s->wild_statement.filename)
  1165.         (void) lookup_name (s->wild_statement.filename);
  1166.       open_input_bfds (s->wild_statement.children.head, force);
  1167.       break;
  1168.     case lang_group_statement_enum:
  1169.       {
  1170.         struct bfd_link_hash_entry *undefs;
  1171.  
  1172.         /* We must continually search the entries in the group
  1173.                until no new symbols are added to the list of undefined
  1174.                symbols.  */
  1175.  
  1176.         do
  1177.           {
  1178.         undefs = link_info.hash->undefs_tail;
  1179.         open_input_bfds (s->group_statement.children.head, true);
  1180.           }
  1181.         while (undefs != link_info.hash->undefs_tail);
  1182.       }
  1183.       break;
  1184.     case lang_target_statement_enum:
  1185.       current_target = s->target_statement.target;
  1186.       break;
  1187.     case lang_input_statement_enum:
  1188.       if (s->input_statement.real == true)
  1189.         {
  1190.           lang_statement_list_type add;
  1191.  
  1192.           s->input_statement.target = current_target;
  1193.  
  1194.           /* If we are being called from within a group, and this
  1195.                  is an archive which has already been searched, then
  1196.                  force it to be researched.  */
  1197.           if (force
  1198.           && s->input_statement.loaded
  1199.           && bfd_check_format (s->input_statement.the_bfd,
  1200.                        bfd_archive))
  1201.         s->input_statement.loaded = false;
  1202.  
  1203.           lang_list_init (&add);
  1204.  
  1205.           load_symbols (&s->input_statement, &add);
  1206.  
  1207.           if (add.head != NULL)
  1208.         {
  1209.           *add.tail = s->next;
  1210.           s->next = add.head;
  1211.         }
  1212.         }
  1213.       break;
  1214.     default:
  1215.       break;
  1216.     }
  1217.     }
  1218. }
  1219.  
  1220. /* If there are [COMMONS] statements, put a wild one into the bss section */
  1221.  
  1222. static void
  1223. lang_reasonable_defaults ()
  1224. {
  1225. #if 0
  1226.   lang_output_section_statement_lookup (".text");
  1227.   lang_output_section_statement_lookup (".data");
  1228.  
  1229.   default_common_section =
  1230.     lang_output_section_statement_lookup (".bss");
  1231.  
  1232.  
  1233.   if (placed_commons == false)
  1234.     {
  1235.       lang_wild_statement_type *new =
  1236.       new_stat (lang_wild_statement,
  1237.         &default_common_section->children);
  1238.  
  1239.       new->section_name = "COMMON";
  1240.       new->filename = (char *) NULL;
  1241.       lang_list_init (&new->children);
  1242.     }
  1243. #endif
  1244.  
  1245. }
  1246.  
  1247. /*
  1248.  Add the supplied name to the symbol table as an undefined reference.
  1249.  Remove items from the chain as we open input bfds
  1250.  */
  1251. typedef struct ldlang_undef_chain_list
  1252. {
  1253.   struct ldlang_undef_chain_list *next;
  1254.   char *name;
  1255. }                       ldlang_undef_chain_list_type;
  1256.  
  1257. static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
  1258.  
  1259. void
  1260. ldlang_add_undef (name)
  1261.      CONST char *CONST name;
  1262. {
  1263.   ldlang_undef_chain_list_type *new =
  1264.     ((ldlang_undef_chain_list_type *)
  1265.      stat_alloc (sizeof (ldlang_undef_chain_list_type)));
  1266.  
  1267.   new->next = ldlang_undef_chain_list_head;
  1268.   ldlang_undef_chain_list_head = new;
  1269.  
  1270.   new->name = buystring (name);
  1271. }
  1272.  
  1273. /* Run through the list of undefineds created above and place them
  1274.    into the linker hash table as undefined symbols belonging to the
  1275.    script file.
  1276. */
  1277. static void
  1278. lang_place_undefineds ()
  1279. {
  1280.   ldlang_undef_chain_list_type *ptr;
  1281.  
  1282.   for (ptr = ldlang_undef_chain_list_head;
  1283.        ptr != (ldlang_undef_chain_list_type *) NULL;
  1284.        ptr = ptr->next)
  1285.     {
  1286.       struct bfd_link_hash_entry *h;
  1287.  
  1288.       h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
  1289.       if (h == (struct bfd_link_hash_entry *) NULL)
  1290.     einfo ("%P%F: bfd_link_hash_lookup failed: %E");
  1291.       if (h->type == bfd_link_hash_new)
  1292.     {
  1293.       h->type = bfd_link_hash_undefined;
  1294.       h->u.undef.abfd = NULL;
  1295.       bfd_link_add_undef (link_info.hash, h);
  1296.     }
  1297.     }
  1298. }
  1299.  
  1300. /* Open input files and attatch to output sections */
  1301. static void
  1302. map_input_to_output_sections (s, target, output_section_statement)
  1303.      lang_statement_union_type * s;
  1304.      CONST char *target;
  1305.      lang_output_section_statement_type * output_section_statement;
  1306. {
  1307.   for (; s != (lang_statement_union_type *) NULL; s = s->next)
  1308.     {
  1309.       switch (s->header.type)
  1310.     {
  1311.  
  1312.  
  1313.     case lang_wild_statement_enum:
  1314.       wild (&s->wild_statement, s->wild_statement.section_name,
  1315.         s->wild_statement.filename, target,
  1316.         output_section_statement);
  1317.  
  1318.       break;
  1319.     case lang_constructors_statement_enum:
  1320.       map_input_to_output_sections (constructor_list.head,
  1321.                     target,
  1322.                     output_section_statement);
  1323.       break;
  1324.     case lang_output_section_statement_enum:
  1325.       map_input_to_output_sections (s->output_section_statement.children.head,
  1326.                     target,
  1327.                     &s->output_section_statement);
  1328.       break;
  1329.     case lang_output_statement_enum:
  1330.       break;
  1331.     case lang_target_statement_enum:
  1332.       target = s->target_statement.target;
  1333.       break;
  1334.     case lang_group_statement_enum:
  1335.       map_input_to_output_sections (s->group_statement.children.head,
  1336.                     target,
  1337.                     output_section_statement);
  1338.       break;
  1339.     case lang_fill_statement_enum:
  1340.     case lang_input_section_enum:
  1341.     case lang_object_symbols_statement_enum:
  1342.     case lang_data_statement_enum:
  1343.     case lang_reloc_statement_enum:
  1344.     case lang_padding_statement_enum:
  1345.     case lang_input_statement_enum:
  1346.       if (output_section_statement != NULL
  1347.           && output_section_statement->bfd_section == NULL)
  1348.         init_os (output_section_statement);
  1349.       break;
  1350.     case lang_assignment_statement_enum:
  1351.       if (output_section_statement != NULL
  1352.           && output_section_statement->bfd_section == NULL)
  1353.         init_os (output_section_statement);
  1354.  
  1355.       /* Make sure that any sections mentioned in the assignment
  1356.              are initialized.  */
  1357.       exp_init_os (s->assignment_statement.exp);
  1358.       break;
  1359.     case lang_afile_asection_pair_statement_enum:
  1360.       FAIL ();
  1361.       break;
  1362.     case lang_address_statement_enum:
  1363.       /* Mark the specified section with the supplied address */
  1364.       {
  1365.         lang_output_section_statement_type *os =
  1366.           lang_output_section_statement_lookup
  1367.         (s->address_statement.section_name);
  1368.  
  1369.         if (os->bfd_section == NULL)
  1370.           init_os (os);
  1371.         os->addr_tree = s->address_statement.address;
  1372.       }
  1373.       break;
  1374.     }
  1375.     }
  1376. }
  1377.  
  1378. static void
  1379. print_output_section_statement (output_section_statement)
  1380.      lang_output_section_statement_type * output_section_statement;
  1381. {
  1382.   asection *section = output_section_statement->bfd_section;
  1383.   int len;
  1384.  
  1385.   if (output_section_statement != abs_output_section)
  1386.     {
  1387.       minfo ("\n%s", output_section_statement->name);
  1388.  
  1389.       if (section != NULL)
  1390.     {
  1391.       print_dot = section->vma;
  1392.  
  1393.       len = strlen (output_section_statement->name);
  1394.       if (len >= SECTION_NAME_MAP_LENGTH - 1)
  1395.         {
  1396.           print_nl ();
  1397.           len = 0;
  1398.         }
  1399.       while (len < SECTION_NAME_MAP_LENGTH)
  1400.         {
  1401.           print_space ();
  1402.           ++len;
  1403.         }
  1404.  
  1405.       minfo ("0x%V %W", section->vma, section->_raw_size);
  1406.  
  1407.       if (output_section_statement->load_base != NULL)
  1408.         {
  1409.           bfd_vma addr;
  1410.  
  1411.           addr = exp_get_abs_int (output_section_statement->load_base, 0,
  1412.                       "load base", lang_final_phase_enum);
  1413.           minfo (" load address 0x%V", addr);
  1414.         }
  1415.     }
  1416.  
  1417.       print_nl ();
  1418.     }
  1419.  
  1420.   print_statement_list (output_section_statement->children.head,
  1421.             output_section_statement);
  1422. }
  1423.  
  1424. static void
  1425. print_assignment (assignment, output_section)
  1426.      lang_assignment_statement_type * assignment;
  1427.      lang_output_section_statement_type * output_section;
  1428. {
  1429.   int i;
  1430.   etree_value_type result;
  1431.  
  1432.   for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
  1433.     print_space ();
  1434.  
  1435.   result = exp_fold_tree (assignment->exp->assign.src, output_section,
  1436.               lang_final_phase_enum, print_dot, &print_dot);
  1437.   if (result.valid)
  1438.     minfo ("0x%V", result.value + result.section->bfd_section->vma);
  1439.   else
  1440.     {
  1441.       minfo ("*undef*   ");
  1442. #ifdef BFD64
  1443.       minfo ("        ");
  1444. #endif
  1445.     }
  1446.  
  1447.   minfo ("                ");
  1448.  
  1449.   exp_print_tree (assignment->exp);
  1450.  
  1451.   print_nl ();
  1452. }
  1453.  
  1454. static void
  1455. print_input_statement (statm)
  1456.      lang_input_statement_type * statm;
  1457. {
  1458.   if (statm->filename != (char *) NULL)
  1459.     {
  1460.       fprintf (config.map_file, "LOAD %s\n", statm->filename);
  1461.     }
  1462. }
  1463.  
  1464. /* Print all symbols defined in a particular section.  This is called
  1465.    via bfd_link_hash_traverse.  */
  1466.  
  1467. static boolean 
  1468. print_one_symbol (hash_entry, ptr)
  1469.      struct bfd_link_hash_entry *hash_entry;
  1470.      PTR ptr;
  1471. {
  1472.   asection *sec = (asection *) ptr;
  1473.  
  1474.   if ((hash_entry->type == bfd_link_hash_defined
  1475.        || hash_entry->type == bfd_link_hash_defweak)
  1476.       && sec == hash_entry->u.def.section)
  1477.     {
  1478.       int i;
  1479.  
  1480.       for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
  1481.     print_space ();
  1482.       minfo ("0x%V   ",
  1483.          (hash_entry->u.def.value
  1484.           + hash_entry->u.def.section->output_offset
  1485.           + hash_entry->u.def.section->output_section->vma));
  1486.  
  1487.       minfo ("             %T\n", hash_entry->root.string);
  1488.     }
  1489.  
  1490.   return true;
  1491. }
  1492.  
  1493. /* Print information about an input section to the map file.  */
  1494.  
  1495. static void
  1496. print_input_section (in)
  1497.      lang_input_section_type * in;
  1498. {
  1499.   asection *i = in->section;
  1500.   bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
  1501.  
  1502.   if (size != 0)
  1503.     {
  1504.       print_space ();
  1505.  
  1506.       minfo ("%s", i->name);
  1507.  
  1508.       if (i->output_section != NULL)
  1509.     {
  1510.       int len;
  1511.  
  1512.       len = 1 + strlen (i->name);
  1513.       if (len >= SECTION_NAME_MAP_LENGTH - 1)
  1514.         {
  1515.           print_nl ();
  1516.           len = 0;
  1517.         }
  1518.       while (len < SECTION_NAME_MAP_LENGTH)
  1519.         {
  1520.           print_space ();
  1521.           ++len;
  1522.         }
  1523.  
  1524.       minfo ("0x%V %W %B\n",
  1525.          i->output_section->vma + i->output_offset, size,
  1526.          i->owner);
  1527.  
  1528.       if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
  1529.         {
  1530.           len = SECTION_NAME_MAP_LENGTH + 3;
  1531. #ifdef BFD64
  1532.           len += 16;
  1533. #else
  1534.           len += 8;
  1535. #endif
  1536.           while (len > 0)
  1537.         {
  1538.           print_space ();
  1539.           --len;
  1540.         }
  1541.  
  1542.           minfo ("%W (size before relaxing)\n", i->_raw_size);
  1543.         }
  1544.  
  1545.       bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
  1546.  
  1547.       print_dot = i->output_section->vma + i->output_offset + size;
  1548.     }
  1549.     }
  1550. }
  1551.  
  1552. static void
  1553. print_fill_statement (fill)
  1554.      lang_fill_statement_type * fill;
  1555. {
  1556.   fprintf (config.map_file, " FILL mask 0x%x\n", fill->fill);
  1557. }
  1558.  
  1559. static void
  1560. print_data_statement (data)
  1561.      lang_data_statement_type * data;
  1562. {
  1563.   int i;
  1564.   bfd_vma addr;
  1565.   bfd_size_type size;
  1566.   const char *name;
  1567.  
  1568.   for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
  1569.     print_space ();
  1570.  
  1571.   addr = data->output_vma;
  1572.   if (data->output_section != NULL)
  1573.     addr += data->output_section->vma;
  1574.  
  1575.   switch (data->type)
  1576.     {
  1577.     default:
  1578.       abort ();
  1579.     case BYTE:
  1580.       size = BYTE_SIZE;
  1581.       name = "BYTE";
  1582.       break;
  1583.     case SHORT:
  1584.       size = SHORT_SIZE;
  1585.       name = "SHORT";
  1586.       break;
  1587.     case LONG:
  1588.       size = LONG_SIZE;
  1589.       name = "LONG";
  1590.       break;
  1591.     case QUAD:
  1592.       size = QUAD_SIZE;
  1593.       name = "QUAD";
  1594.       break;
  1595.     }
  1596.  
  1597.   minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
  1598.  
  1599.   if (data->exp->type.node_class != etree_value)
  1600.     {
  1601.       print_space ();
  1602.       exp_print_tree (data->exp);
  1603.     }
  1604.  
  1605.   print_nl ();
  1606.  
  1607.   print_dot = addr + size;
  1608. }
  1609.  
  1610. /* Print an address statement.  These are generated by options like
  1611.    -Ttext.  */
  1612.  
  1613. static void
  1614. print_address_statement (address)
  1615.      lang_address_statement_type *address;
  1616. {
  1617.   minfo ("Address of section %s set to ", address->section_name);
  1618.   exp_print_tree (address->address);
  1619.   print_nl ();
  1620. }
  1621.  
  1622. /* Print a reloc statement.  */
  1623.  
  1624. static void
  1625. print_reloc_statement (reloc)
  1626.      lang_reloc_statement_type *reloc;
  1627. {
  1628.   int i;
  1629.   bfd_vma addr;
  1630.   bfd_size_type size;
  1631.  
  1632.   for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
  1633.     print_space ();
  1634.  
  1635.   addr = reloc->output_vma;
  1636.   if (reloc->output_section != NULL)
  1637.     addr += reloc->output_section->vma;
  1638.  
  1639.   size = bfd_get_reloc_size (reloc->howto);
  1640.  
  1641.   minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
  1642.  
  1643.   if (reloc->name != NULL)
  1644.     minfo ("%s+", reloc->name);
  1645.   else
  1646.     minfo ("%s+", reloc->section->name);
  1647.  
  1648.   exp_print_tree (reloc->addend_exp);
  1649.  
  1650.   print_nl ();
  1651.  
  1652.   print_dot = addr + size;
  1653. }  
  1654.  
  1655. static void
  1656. print_padding_statement (s)
  1657.      lang_padding_statement_type *s;
  1658. {
  1659.   int len;
  1660.   bfd_vma addr;
  1661.  
  1662.   minfo (" *fill*");
  1663.  
  1664.   len = sizeof " *fill*" - 1;
  1665.   while (len < SECTION_NAME_MAP_LENGTH)
  1666.     {
  1667.       print_space ();
  1668.       ++len;
  1669.     }
  1670.  
  1671.   addr = s->output_offset;
  1672.   if (s->output_section != NULL)
  1673.     addr += s->output_section->vma;
  1674.   minfo ("0x%V %W", addr, s->size);
  1675.  
  1676.   if (s->fill != 0)
  1677.     minfo (" %u", s->fill);
  1678.  
  1679.   print_nl ();
  1680.  
  1681.   print_dot = addr + s->size;
  1682. }
  1683.  
  1684. static void
  1685. print_wild_statement (w, os)
  1686.      lang_wild_statement_type * w;
  1687.      lang_output_section_statement_type * os;
  1688. {
  1689.   print_space ();
  1690.  
  1691.   if (w->filename != NULL)
  1692.     minfo ("%s", w->filename);
  1693.   else
  1694.     minfo ("*");
  1695.  
  1696.   if (w->section_name != NULL)
  1697.     minfo ("(%s)", w->section_name);
  1698.   else
  1699.     minfo ("(*)");
  1700.  
  1701.   print_nl ();
  1702.  
  1703.   print_statement_list (w->children.head, os);
  1704. }
  1705.  
  1706. /* Print a group statement.  */
  1707.  
  1708. static void
  1709. print_group (s, os)
  1710.      lang_group_statement_type *s;
  1711.      lang_output_section_statement_type *os;
  1712. {
  1713.   fprintf (config.map_file, "START GROUP\n");
  1714.   print_statement_list (s->children.head, os);
  1715.   fprintf (config.map_file, "END GROUP\n");
  1716. }
  1717.  
  1718. /* Print the list of statements in S.
  1719.    This can be called for any statement type.  */
  1720.  
  1721. static void
  1722. print_statement_list (s, os)
  1723.      lang_statement_union_type *s;
  1724.      lang_output_section_statement_type *os;
  1725. {
  1726.   while (s != NULL)
  1727.     {
  1728.       print_statement (s, os);
  1729.       s = s->next;
  1730.     }
  1731. }
  1732.  
  1733. /* Print the first statement in statement list S.
  1734.    This can be called for any statement type.  */
  1735.  
  1736. static void
  1737. print_statement (s, os)
  1738.      lang_statement_union_type *s;
  1739.      lang_output_section_statement_type *os;
  1740. {
  1741.   switch (s->header.type)
  1742.     {
  1743.     default:
  1744.       fprintf (config.map_file, "Fail with %d\n", s->header.type);
  1745.       FAIL ();
  1746.       break;
  1747.     case lang_constructors_statement_enum:
  1748.       if (constructor_list.head != NULL)
  1749.     {
  1750.       minfo (" CONSTRUCTORS\n");
  1751.       print_statement_list (constructor_list.head, os);
  1752.     }
  1753.       break;
  1754.     case lang_wild_statement_enum:
  1755.       print_wild_statement (&s->wild_statement, os);
  1756.       break;
  1757.     case lang_address_statement_enum:
  1758.       print_address_statement (&s->address_statement);
  1759.       break;
  1760.     case lang_object_symbols_statement_enum:
  1761.       minfo (" CREATE_OBJECT_SYMBOLS\n");
  1762.       break;
  1763.     case lang_fill_statement_enum:
  1764.       print_fill_statement (&s->fill_statement);
  1765.       break;
  1766.     case lang_data_statement_enum:
  1767.       print_data_statement (&s->data_statement);
  1768.       break;
  1769.     case lang_reloc_statement_enum:
  1770.       print_reloc_statement (&s->reloc_statement);
  1771.       break;
  1772.     case lang_input_section_enum:
  1773.       print_input_section (&s->input_section);
  1774.       break;
  1775.     case lang_padding_statement_enum:
  1776.       print_padding_statement (&s->padding_statement);
  1777.       break;
  1778.     case lang_output_section_statement_enum:
  1779.       print_output_section_statement (&s->output_section_statement);
  1780.       break;
  1781.     case lang_assignment_statement_enum:
  1782.       print_assignment (&s->assignment_statement, os);
  1783.       break;
  1784.     case lang_target_statement_enum:
  1785.       fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
  1786.       break;
  1787.     case lang_output_statement_enum:
  1788.       minfo ("OUTPUT(%s", s->output_statement.name);
  1789.       if (output_target != NULL)
  1790.     minfo (" %s", output_target);
  1791.       minfo (")\n");
  1792.       break;
  1793.     case lang_input_statement_enum:
  1794.       print_input_statement (&s->input_statement);
  1795.       break;
  1796.     case lang_group_statement_enum:
  1797.       print_group (&s->group_statement, os);
  1798.       break;
  1799.     case lang_afile_asection_pair_statement_enum:
  1800.       FAIL ();
  1801.       break;
  1802.     }
  1803. }
  1804.  
  1805. static void
  1806. print_statements ()
  1807. {
  1808.   print_statement_list (statement_list.head, abs_output_section);
  1809. }
  1810.  
  1811. /* Print the first N statements in statement list S to STDERR.
  1812.    If N == 0, nothing is printed.
  1813.    If N < 0, the entire list is printed.
  1814.    Intended to be called from GDB.  */
  1815.  
  1816. void
  1817. dprint_statement (s, n)
  1818.      lang_statement_union_type * s;
  1819.      int n;
  1820. {
  1821.   FILE *map_save = config.map_file;
  1822.  
  1823.   config.map_file = stderr;
  1824.  
  1825.   if (n < 0)
  1826.     print_statement_list (s, abs_output_section);
  1827.   else
  1828.     {
  1829.       while (s && --n >= 0)
  1830.     {
  1831.       print_statement (s, abs_output_section);
  1832.       s = s->next;
  1833.     }
  1834.     }
  1835.  
  1836.   config.map_file = map_save;
  1837. }
  1838.  
  1839. static bfd_vma
  1840. insert_pad (this_ptr, fill, power, output_section_statement, dot)
  1841.      lang_statement_union_type ** this_ptr;
  1842.      fill_type fill;
  1843.      unsigned int power;
  1844.      asection * output_section_statement;
  1845.      bfd_vma dot;
  1846. {
  1847.   /* Align this section first to the
  1848.      input sections requirement, then
  1849.      to the output section's requirement.
  1850.      If this alignment is > than any seen before,
  1851.      then record it too. Perform the alignment by
  1852.      inserting a magic 'padding' statement.
  1853.      */
  1854.  
  1855.   unsigned int alignment_needed = align_power (dot, power) - dot;
  1856.  
  1857.   if (alignment_needed != 0)
  1858.     {
  1859.       lang_statement_union_type *new =
  1860.     ((lang_statement_union_type *)
  1861.      stat_alloc (sizeof (lang_padding_statement_type)));
  1862.  
  1863.       /* Link into existing chain */
  1864.       new->header.next = *this_ptr;
  1865.       *this_ptr = new;
  1866.       new->header.type = lang_padding_statement_enum;
  1867.       new->padding_statement.output_section = output_section_statement;
  1868.       new->padding_statement.output_offset =
  1869.     dot - output_section_statement->vma;
  1870.       new->padding_statement.fill = fill;
  1871.       new->padding_statement.size = alignment_needed;
  1872.     }
  1873.  
  1874.  
  1875.   /* Remember the most restrictive alignment */
  1876.   if (power > output_section_statement->alignment_power)
  1877.     {
  1878.       output_section_statement->alignment_power = power;
  1879.     }
  1880.   output_section_statement->_raw_size += alignment_needed;
  1881.   return alignment_needed + dot;
  1882.  
  1883. }
  1884.  
  1885. /* Work out how much this section will move the dot point */
  1886. static bfd_vma
  1887. size_input_section (this_ptr, output_section_statement, fill, dot, relax)
  1888.      lang_statement_union_type ** this_ptr;
  1889.      lang_output_section_statement_type * output_section_statement;
  1890.      fill_type fill;
  1891.      bfd_vma dot;
  1892.      boolean relax;
  1893. {
  1894.   lang_input_section_type *is = &((*this_ptr)->input_section);
  1895.   asection *i = is->section;
  1896.  
  1897.   if (is->ifile->just_syms_flag == false)
  1898.     {
  1899.       if (output_section_statement->subsection_alignment != -1)
  1900.        i->alignment_power =
  1901.     output_section_statement->subsection_alignment;
  1902.  
  1903.       dot = insert_pad (this_ptr, fill, i->alignment_power,
  1904.             output_section_statement->bfd_section, dot);
  1905.  
  1906.       /* Remember where in the output section this input section goes */
  1907.  
  1908.       i->output_offset = dot - output_section_statement->bfd_section->vma;
  1909.  
  1910.       /* Mark how big the output section must be to contain this now
  1911.      */
  1912.       if (i->_cooked_size != 0)
  1913.     dot += i->_cooked_size;
  1914.       else
  1915.     dot += i->_raw_size;
  1916.       output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
  1917.     }
  1918.   else
  1919.     {
  1920.       i->output_offset = i->vma - output_section_statement->bfd_section->vma;
  1921.     }
  1922.  
  1923.   return dot;
  1924. }
  1925.  
  1926. /* This variable indicates whether bfd_relax_section should be called
  1927.    again.  */
  1928.  
  1929. static boolean relax_again;
  1930.  
  1931. /* Set the sizes for all the output sections.  */
  1932.  
  1933. bfd_vma
  1934. lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
  1935.      lang_statement_union_type * s;
  1936.      lang_output_section_statement_type * output_section_statement;
  1937.      lang_statement_union_type ** prev;
  1938.      fill_type fill;
  1939.      bfd_vma dot;
  1940.      boolean relax;
  1941. {
  1942.   /* Size up the sections from their constituent parts */
  1943.   for (; s != (lang_statement_union_type *) NULL; s = s->next)
  1944.   {
  1945.     switch (s->header.type)
  1946.     {
  1947.  
  1948.      case lang_output_section_statement_enum:
  1949.      {
  1950.        bfd_vma after;
  1951.        lang_output_section_statement_type *os = &s->output_section_statement;
  1952.  
  1953.        if (os->bfd_section == NULL)
  1954.      {
  1955.        /* This section was never actually created.  */
  1956.        break;
  1957.      }
  1958.  
  1959.        /* If this is a COFF shared library section, use the size and
  1960.       address from the input section.  FIXME: This is COFF
  1961.       specific; it would be cleaner if there were some other way
  1962.       to do this, but nothing simple comes to mind.  */
  1963.        if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
  1964.      {
  1965.        asection *input;
  1966.  
  1967.        if (os->children.head == NULL
  1968.            || os->children.head->next != NULL
  1969.            || os->children.head->header.type != lang_input_section_enum)
  1970.          einfo ("%P%X: Internal error on COFF shared library section %s",
  1971.             os->name);
  1972.  
  1973.        input = os->children.head->input_section.section;
  1974.        bfd_set_section_vma (os->bfd_section->owner,
  1975.                 os->bfd_section,
  1976.                 bfd_section_vma (input->owner, input));
  1977.        os->bfd_section->_raw_size = input->_raw_size;
  1978.        break;
  1979.      }
  1980.  
  1981.        if (bfd_is_abs_section (os->bfd_section))
  1982.        {
  1983.      /* No matter what happens, an abs section starts at zero */
  1984.      ASSERT (os->bfd_section->vma == 0);
  1985.        }
  1986.        else
  1987.        {
  1988.      if (os->addr_tree == (etree_type *) NULL)
  1989.      {
  1990.        /* No address specified for this section, get one
  1991.           from the region specification
  1992.           */
  1993.        if (os->region == (lang_memory_region_type *) NULL)
  1994.        {
  1995.          os->region = lang_memory_region_lookup ("*default*");
  1996.        }
  1997.        dot = os->region->current;
  1998.        if (os->section_alignment == -1)
  1999.          dot = align_power (dot, os->bfd_section->alignment_power);
  2000.      }
  2001.      else
  2002.      {
  2003.        etree_value_type r;
  2004.  
  2005.        r = exp_fold_tree (os->addr_tree,
  2006.                   abs_output_section,
  2007.                   lang_allocating_phase_enum,
  2008.                   dot, &dot);
  2009.        if (r.valid == false)
  2010.        {
  2011.          einfo ("%F%S: non constant address expression for section %s\n",
  2012.             os->name);
  2013.        }
  2014.        dot = r.value;
  2015.      }
  2016.      /* The section starts here */
  2017.      /* First, align to what the section needs */
  2018.  
  2019.      if (os->section_alignment != -1)
  2020.        dot = align_power (dot, os->section_alignment);
  2021.  
  2022.      bfd_set_section_vma (0, os->bfd_section, dot);
  2023.      
  2024.      os->bfd_section->output_offset = 0;
  2025.        }
  2026.  
  2027.        (void) lang_size_sections (os->children.head, os, &os->children.head,
  2028.                   os->fill, dot, relax);
  2029.        /* Ignore the size of the input sections, use the vma and size to */
  2030.        /* align against */
  2031.  
  2032.        after = ALIGN_N (os->bfd_section->vma +
  2033.             os->bfd_section->_raw_size,
  2034.             /* The coercion here is important, see ld.h.  */
  2035.             (bfd_vma) os->block_value);
  2036.  
  2037.        if (bfd_is_abs_section (os->bfd_section))
  2038.      ASSERT (after == os->bfd_section->vma);
  2039.        else
  2040.      os->bfd_section->_raw_size = after - os->bfd_section->vma;
  2041.        dot = os->bfd_section->vma + os->bfd_section->_raw_size;
  2042.        os->processed = true;
  2043.  
  2044.        /* Replace into region ? */
  2045.        if (os->region != (lang_memory_region_type *) NULL)
  2046.      {
  2047.        os->region->current = dot;
  2048.        /* Make sure this isn't silly.  */
  2049.        if (os->region->current < os->region->origin
  2050.            || (os->region->current - os->region->origin
  2051.            > os->region->length))
  2052.          {
  2053.            if (os->addr_tree != (etree_type *) NULL)
  2054.          {
  2055.            einfo ("%X%P: address 0x%v of %B section %s is not within region %s\n",
  2056.               os->region->current,
  2057.               os->bfd_section->owner,
  2058.               os->bfd_section->name,
  2059.               os->region->name);
  2060.          }
  2061.            else
  2062.          {
  2063.            einfo ("%X%P: region %s is full (%B section %s)\n",
  2064.               os->region->name,
  2065.               os->bfd_section->owner,
  2066.               os->bfd_section->name);
  2067.          }
  2068.            /* Reset the region pointer.  */
  2069.            os->region->current = os->region->origin;
  2070.          }
  2071.      }
  2072.      }
  2073.      break;
  2074.  
  2075.      case lang_constructors_statement_enum:
  2076.       dot = lang_size_sections (constructor_list.head,
  2077.                 output_section_statement,
  2078.                 &s->wild_statement.children.head,
  2079.                 fill,
  2080.                 dot, relax);
  2081.       break;
  2082.  
  2083.      case lang_data_statement_enum:
  2084.      {
  2085.        unsigned int size = 0;
  2086.  
  2087.        s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
  2088.        s->data_statement.output_section =
  2089.     output_section_statement->bfd_section;
  2090.  
  2091.        switch (s->data_statement.type)
  2092.        {
  2093.         case QUAD:
  2094.      size = QUAD_SIZE;
  2095.      break;
  2096.     case LONG:
  2097.      size = LONG_SIZE;
  2098.      break;
  2099.     case SHORT:
  2100.      size = SHORT_SIZE;
  2101.      break;
  2102.     case BYTE:
  2103.      size = BYTE_SIZE;
  2104.      break;
  2105.  
  2106.        }
  2107.        dot += size;
  2108.        output_section_statement->bfd_section->_raw_size += size;
  2109.        /* The output section gets contents, and then we inspect for
  2110.       any flags set in the input script which override any ALLOC */
  2111.        output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
  2112.        if (!(output_section_statement->flags & SEC_NEVER_LOAD)) {
  2113.      output_section_statement->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
  2114.        }
  2115.      }
  2116.       break;
  2117.  
  2118.      case lang_reloc_statement_enum:
  2119.      {
  2120.        int size;
  2121.  
  2122.        s->reloc_statement.output_vma =
  2123.      dot - output_section_statement->bfd_section->vma;
  2124.        s->reloc_statement.output_section =
  2125.      output_section_statement->bfd_section;
  2126.        size = bfd_get_reloc_size (s->reloc_statement.howto);
  2127.        dot += size;
  2128.        output_section_statement->bfd_section->_raw_size += size;
  2129.      }
  2130.      break;
  2131.      
  2132.      case lang_wild_statement_enum:
  2133.  
  2134.       dot = lang_size_sections (s->wild_statement.children.head,
  2135.                 output_section_statement,
  2136.                 &s->wild_statement.children.head,
  2137.  
  2138.                 fill, dot, relax);
  2139.  
  2140.       break;
  2141.  
  2142.      case lang_object_symbols_statement_enum:
  2143.       link_info.create_object_symbols_section =
  2144.     output_section_statement->bfd_section;
  2145.       break;
  2146.      case lang_output_statement_enum:
  2147.      case lang_target_statement_enum:
  2148.       break;
  2149.      case lang_input_section_enum:
  2150.       {
  2151.     asection *i;
  2152.  
  2153.     i = (*prev)->input_section.section;
  2154.     if (! relax)
  2155.       {
  2156.         if (i->_cooked_size == 0)
  2157.           i->_cooked_size = i->_raw_size;
  2158.       }
  2159.     else
  2160.       {
  2161.         boolean again;
  2162.  
  2163.         if (! bfd_relax_section (i->owner, i, &link_info, &again))
  2164.           einfo ("%P%F: can't relax section: %E\n");
  2165.         if (again)
  2166.           relax_again = true;
  2167.       }
  2168.     dot = size_input_section (prev,
  2169.                   output_section_statement,
  2170.                   output_section_statement->fill,
  2171.                   dot, relax);
  2172.       }
  2173.       break;
  2174.      case lang_input_statement_enum:
  2175.       break;
  2176.      case lang_fill_statement_enum:
  2177.       s->fill_statement.output_section = output_section_statement->bfd_section;
  2178.  
  2179.       fill = s->fill_statement.fill;
  2180.       break;
  2181.      case lang_assignment_statement_enum:
  2182.      {
  2183.        bfd_vma newdot = dot;
  2184.  
  2185.        exp_fold_tree (s->assignment_statement.exp,
  2186.               output_section_statement,
  2187.               lang_allocating_phase_enum,
  2188.               dot,
  2189.               &newdot);
  2190.  
  2191.        if (newdot != dot && !relax)
  2192.      {
  2193.        /* The assignment changed dot.  Insert a pad.  */
  2194.        if (output_section_statement == abs_output_section)
  2195.          {
  2196.            /* If we don't have an output section, then just adjust
  2197.           the default memory address.  */
  2198.            lang_memory_region_lookup ("*default*")->current = newdot;
  2199.          }
  2200.        else
  2201.          {
  2202.            lang_statement_union_type *new =
  2203.          ((lang_statement_union_type *)
  2204.           stat_alloc (sizeof (lang_padding_statement_type)));
  2205.  
  2206.            /* Link into existing chain */
  2207.            new->header.next = *prev;
  2208.            *prev = new;
  2209.            new->header.type = lang_padding_statement_enum;
  2210.            new->padding_statement.output_section =
  2211.          output_section_statement->bfd_section;
  2212.            new->padding_statement.output_offset =
  2213.          dot - output_section_statement->bfd_section->vma;
  2214.            new->padding_statement.fill = fill;
  2215.            new->padding_statement.size = newdot - dot;
  2216.            output_section_statement->bfd_section->_raw_size +=
  2217.          new->padding_statement.size;
  2218.          }
  2219.  
  2220.        dot = newdot;
  2221.      }
  2222.      }
  2223.      break;
  2224.  
  2225.    case lang_padding_statement_enum:
  2226.      /* If we are relaxing, and this is not the first pass, some
  2227.     padding statements may have been inserted during previous
  2228.     passes.  We may have to move the padding statement to a new
  2229.     location if dot has a different value at this point in this
  2230.     pass than it did at this point in the previous pass.  */
  2231.      s->padding_statement.output_offset =
  2232.        dot - output_section_statement->bfd_section->vma;
  2233.      dot += s->padding_statement.size;
  2234.      output_section_statement->bfd_section->_raw_size +=
  2235.        s->padding_statement.size;
  2236.      break;
  2237.  
  2238.      case lang_group_statement_enum:
  2239.        dot = lang_size_sections (s->group_statement.children.head,
  2240.                  output_section_statement,
  2241.                  &s->group_statement.children.head,
  2242.                  fill, dot, relax);
  2243.        break;
  2244.  
  2245.      default:
  2246.       FAIL ();
  2247.       break;
  2248.  
  2249.       /* This can only get here when relaxing is turned on */
  2250.  
  2251.      case lang_address_statement_enum:
  2252.       break;
  2253.     }
  2254.     prev = &s->header.next;
  2255.   }
  2256.   return dot;
  2257. }
  2258.  
  2259. bfd_vma
  2260. lang_do_assignments (s, output_section_statement, fill, dot)
  2261.      lang_statement_union_type * s;
  2262.      lang_output_section_statement_type * output_section_statement;
  2263.      fill_type fill;
  2264.      bfd_vma dot;
  2265. {
  2266.   for (; s != (lang_statement_union_type *) NULL; s = s->next)
  2267.     {
  2268.       switch (s->header.type)
  2269.     {
  2270.     case lang_constructors_statement_enum:
  2271.       dot = lang_do_assignments (constructor_list.head,
  2272.                      output_section_statement,
  2273.                      fill,
  2274.                      dot);
  2275.       break;
  2276.  
  2277.     case lang_output_section_statement_enum:
  2278.       {
  2279.         lang_output_section_statement_type *os =
  2280.           &(s->output_section_statement);
  2281.  
  2282.         if (os->bfd_section != NULL)
  2283.           {
  2284.         dot = os->bfd_section->vma;
  2285.         (void) lang_do_assignments (os->children.head, os,
  2286.                         os->fill, dot);
  2287.         dot = os->bfd_section->vma + os->bfd_section->_raw_size;
  2288.           }
  2289.         if (os->load_base) 
  2290.           {
  2291.         /* If nothing has been placed into the output section then
  2292.            it won't have a bfd_section. */
  2293.         if (os->bfd_section) 
  2294.           {
  2295.             os->bfd_section->lma 
  2296.               = exp_get_abs_int(os->load_base, 0,"load base", lang_final_phase_enum);
  2297.           }
  2298.           }
  2299.       }
  2300.       break;
  2301.     case lang_wild_statement_enum:
  2302.  
  2303.       dot = lang_do_assignments (s->wild_statement.children.head,
  2304.                      output_section_statement,
  2305.                      fill, dot);
  2306.  
  2307.       break;
  2308.  
  2309.     case lang_object_symbols_statement_enum:
  2310.     case lang_output_statement_enum:
  2311.     case lang_target_statement_enum:
  2312. #if 0
  2313.     case lang_common_statement_enum:
  2314. #endif
  2315.       break;
  2316.     case lang_data_statement_enum:
  2317.       {
  2318.         etree_value_type value;
  2319.  
  2320.         value = exp_fold_tree (s->data_statement.exp,
  2321.                    abs_output_section,
  2322.                    lang_final_phase_enum, dot, &dot);
  2323.         s->data_statement.value = value.value;
  2324.         if (value.valid == false)
  2325.           einfo ("%F%P: invalid data statement\n");
  2326.       }
  2327.       switch (s->data_statement.type)
  2328.         {
  2329.         case QUAD:
  2330.           dot += QUAD_SIZE;
  2331.           break;
  2332.         case LONG:
  2333.           dot += LONG_SIZE;
  2334.           break;
  2335.         case SHORT:
  2336.           dot += SHORT_SIZE;
  2337.           break;
  2338.         case BYTE:
  2339.           dot += BYTE_SIZE;
  2340.           break;
  2341.         }
  2342.       break;
  2343.  
  2344.     case lang_reloc_statement_enum:
  2345.       {
  2346.         etree_value_type value;
  2347.  
  2348.         value = exp_fold_tree (s->reloc_statement.addend_exp,
  2349.                    abs_output_section,
  2350.                    lang_final_phase_enum, dot, &dot);
  2351.         s->reloc_statement.addend_value = value.value;
  2352.         if (value.valid == false)
  2353.           einfo ("%F%P: invalid reloc statement\n");
  2354.       }
  2355.       dot += bfd_get_reloc_size (s->reloc_statement.howto);
  2356.       break;
  2357.  
  2358.     case lang_input_section_enum:
  2359.       {
  2360.         asection *in = s->input_section.section;
  2361.  
  2362.         if (in->_cooked_size != 0)
  2363.           dot += in->_cooked_size;
  2364.         else
  2365.           dot += in->_raw_size;
  2366.       }
  2367.       break;
  2368.  
  2369.     case lang_input_statement_enum:
  2370.       break;
  2371.     case lang_fill_statement_enum:
  2372.       fill = s->fill_statement.fill;
  2373.       break;
  2374.     case lang_assignment_statement_enum:
  2375.       {
  2376.         exp_fold_tree (s->assignment_statement.exp,
  2377.                output_section_statement,
  2378.                lang_final_phase_enum,
  2379.                dot,
  2380.                &dot);
  2381.       }
  2382.  
  2383.       break;
  2384.     case lang_padding_statement_enum:
  2385.       dot += s->padding_statement.size;
  2386.       break;
  2387.  
  2388.     case lang_group_statement_enum:
  2389.       dot = lang_do_assignments (s->group_statement.children.head,
  2390.                      output_section_statement,
  2391.                      fill, dot);
  2392.  
  2393.       break;
  2394.  
  2395.     default:
  2396.       FAIL ();
  2397.       break;
  2398.     case lang_address_statement_enum:
  2399.       break;
  2400.     }
  2401.  
  2402.     }
  2403.   return dot;
  2404. }
  2405.  
  2406. /* Fix any .startof. or .sizeof. symbols.  When the assemblers see the
  2407.    operator .startof. (section_name), it produces an undefined symbol
  2408.    .startof.section_name.  Similarly, when it sees
  2409.    .sizeof. (section_name), it produces an undefined symbol
  2410.    .sizeof.section_name.  For all the output sections, we look for
  2411.    such symbols, and set them to the correct value.  */
  2412.  
  2413. static void
  2414. lang_set_startof ()
  2415. {
  2416.   asection *s;
  2417.  
  2418.   if (link_info.relocateable)
  2419.     return;
  2420.  
  2421.   for (s = output_bfd->sections; s != NULL; s = s->next)
  2422.     {
  2423.       const char *secname;
  2424.       char *buf;
  2425.       struct bfd_link_hash_entry *h;
  2426.  
  2427.       secname = bfd_get_section_name (output_bfd, s);
  2428.       buf = xmalloc (10 + strlen (secname));
  2429.  
  2430.       sprintf (buf, ".startof.%s", secname);
  2431.       h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
  2432.       if (h != NULL && h->type == bfd_link_hash_undefined)
  2433.     {
  2434.       h->type = bfd_link_hash_defined;
  2435.       h->u.def.value = bfd_get_section_vma (output_bfd, s);
  2436.       h->u.def.section = bfd_abs_section_ptr;
  2437.     }
  2438.  
  2439.       sprintf (buf, ".sizeof.%s", secname);
  2440.       h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
  2441.       if (h != NULL && h->type == bfd_link_hash_undefined)
  2442.     {
  2443.       h->type = bfd_link_hash_defined;
  2444.       if (s->_cooked_size != 0)
  2445.         h->u.def.value = s->_cooked_size;
  2446.       else
  2447.         h->u.def.value = s->_raw_size;
  2448.       h->u.def.section = bfd_abs_section_ptr;
  2449.     }
  2450.  
  2451.       free (buf);
  2452.     }
  2453. }
  2454.  
  2455. static void
  2456. lang_finish ()
  2457. {
  2458.   struct bfd_link_hash_entry *h;
  2459.   boolean warn;
  2460.  
  2461.   if (link_info.relocateable || link_info.shared)
  2462.     warn = false;
  2463.   else
  2464.     warn = true;
  2465.  
  2466.   if (entry_symbol == (char *) NULL)
  2467.     {
  2468.       /* No entry has been specified.  Look for start, but don't warn
  2469.      if we don't find it.  */
  2470.       entry_symbol = "start";
  2471.       warn = false;
  2472.     }
  2473.  
  2474.   h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
  2475.   if (h != (struct bfd_link_hash_entry *) NULL
  2476.       && (h->type == bfd_link_hash_defined
  2477.       || h->type == bfd_link_hash_defweak)
  2478.       && h->u.def.section->output_section != NULL)
  2479.     {
  2480.       bfd_vma val;
  2481.  
  2482.       val = (h->u.def.value
  2483.          + bfd_get_section_vma (output_bfd,
  2484.                     h->u.def.section->output_section)
  2485.          + h->u.def.section->output_offset);
  2486.       if (! bfd_set_start_address (output_bfd, val))
  2487.     einfo ("%P%F:%s: can't set start address\n", entry_symbol);
  2488.     }
  2489.   else
  2490.     {
  2491.       asection *ts;
  2492.  
  2493.       /* Can't find the entry symbol.  Use the first address in the
  2494.      text section.  */
  2495.       ts = bfd_get_section_by_name (output_bfd, ".text");
  2496.       if (ts != (asection *) NULL)
  2497.     {
  2498.       if (warn)
  2499.         einfo ("%P: warning: cannot find entry symbol %s; defaulting to %V\n",
  2500.            entry_symbol, bfd_get_section_vma (output_bfd, ts));
  2501.       if (! bfd_set_start_address (output_bfd,
  2502.                        bfd_get_section_vma (output_bfd, ts)))
  2503.         einfo ("%P%F: can't set start address\n");
  2504.     }
  2505.       else
  2506.     {
  2507.       if (warn)
  2508.         einfo ("%P: warning: cannot find entry symbol %s; not setting start address\n",
  2509.            entry_symbol);
  2510.     }
  2511.     }
  2512. }
  2513.  
  2514. /* Check that the architecture of all the input files is compatible
  2515.    with the output file.  Also call the backend to let it do any
  2516.    other checking that is needed.  */
  2517.  
  2518. static void
  2519. lang_check ()
  2520. {
  2521.   lang_statement_union_type *file;
  2522.   bfd *input_bfd;
  2523.   CONST bfd_arch_info_type *compatible;
  2524.  
  2525.   for (file = file_chain.head;
  2526.        file != (lang_statement_union_type *) NULL;
  2527.        file = file->input_statement.next)
  2528.     {
  2529.       input_bfd = file->input_statement.the_bfd;
  2530.       compatible = bfd_arch_get_compatible (input_bfd,
  2531.                         output_bfd);
  2532.       if (compatible == NULL)
  2533.     einfo ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
  2534.            bfd_printable_name (input_bfd), input_bfd,
  2535.            bfd_printable_name (output_bfd));
  2536.  
  2537.       else
  2538.     bfd_merge_private_bfd_data (input_bfd, output_bfd);
  2539.     }
  2540. }
  2541.  
  2542. /* Look through all the global common symbols and attach them to the
  2543.    correct section.  The -sort-common command line switch may be used
  2544.    to roughly sort the entries by size.  */
  2545.  
  2546. static void
  2547. lang_common ()
  2548. {
  2549.   if (link_info.relocateable
  2550.       && ! command_line.force_common_definition)
  2551.     return;
  2552.  
  2553.   if (! config.sort_common)
  2554.     bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
  2555.   else
  2556.     {
  2557.       int power;
  2558.  
  2559.       for (power = 4; power >= 0; power--)
  2560.     bfd_link_hash_traverse (link_info.hash, lang_one_common,
  2561.                 (PTR) &power);
  2562.     }
  2563. }
  2564.  
  2565. /* Place one common symbol in the correct section.  */
  2566.  
  2567. static boolean
  2568. lang_one_common (h, info)
  2569.      struct bfd_link_hash_entry *h;
  2570.      PTR info;
  2571. {
  2572.   unsigned int power_of_two;
  2573.   bfd_vma size;
  2574.   asection *section;
  2575.  
  2576.   if (h->type != bfd_link_hash_common)
  2577.     return true;
  2578.  
  2579.   size = h->u.c.size;
  2580.   power_of_two = h->u.c.p->alignment_power;
  2581.  
  2582.   if (config.sort_common
  2583.       && power_of_two < (unsigned int) *(int *) info)
  2584.     return true;
  2585.  
  2586.   section = h->u.c.p->section;
  2587.  
  2588.   /* Increase the size of the section.  */
  2589.   section->_raw_size = ALIGN_N (section->_raw_size,
  2590.                 (bfd_size_type) (1 << power_of_two));
  2591.  
  2592.   /* Adjust the alignment if necessary.  */
  2593.   if (power_of_two > section->alignment_power)
  2594.     section->alignment_power = power_of_two;
  2595.  
  2596.   /* Change the symbol from common to defined.  */
  2597.   h->type = bfd_link_hash_defined;
  2598.   h->u.def.section = section;
  2599.   h->u.def.value = section->_raw_size;
  2600.  
  2601.   /* Increase the size of the section.  */
  2602.   section->_raw_size += size;
  2603.  
  2604.   /* Make sure the section is allocated in memory.  */
  2605.   section->flags |= SEC_ALLOC;
  2606.  
  2607.   if (config.map_file != NULL)
  2608.     {
  2609.       static boolean header_printed;
  2610.       int len;
  2611.       char *name;
  2612.       char buf[50];
  2613.  
  2614.       if (! header_printed)
  2615.     {
  2616.       minfo ("\nAllocating common symbols\n");
  2617.       minfo ("Common symbol       size              file\n\n");
  2618.       header_printed = true;
  2619.     }
  2620.  
  2621.       name = demangle (h->root.string);
  2622.       minfo ("%s", name);
  2623.       len = strlen (name);
  2624.       free (name);
  2625.  
  2626.       if (len >= 19)
  2627.     {
  2628.       print_nl ();
  2629.       len = 0;
  2630.     }
  2631.       while (len < 20)
  2632.     {
  2633.       print_space ();
  2634.       ++len;
  2635.     }
  2636.  
  2637.       minfo ("0x");
  2638.       if (size <= 0xffffffff)
  2639.     sprintf (buf, "%lx", (unsigned long) size);
  2640.       else
  2641.     sprintf_vma (buf, size);
  2642.       minfo ("%s", buf);
  2643.       len = strlen (buf);
  2644.  
  2645.       while (len < 16)
  2646.     {
  2647.       print_space ();
  2648.       ++len;
  2649.     }
  2650.  
  2651.       minfo ("%B\n", section->owner);
  2652.     }
  2653.  
  2654.   return true;
  2655. }
  2656.  
  2657. /*
  2658. run through the input files and ensure that every input
  2659. section has somewhere to go. If one is found without
  2660. a destination then create an input request and place it
  2661. into the statement tree.
  2662. */
  2663.  
  2664. static void
  2665. lang_place_orphans ()
  2666. {
  2667.   lang_input_statement_type *file;
  2668.  
  2669.   for (file = (lang_input_statement_type *) file_chain.head;
  2670.        file != (lang_input_statement_type *) NULL;
  2671.        file = (lang_input_statement_type *) file->next)
  2672.     {
  2673.       asection *s;
  2674.  
  2675.       for (s = file->the_bfd->sections;
  2676.        s != (asection *) NULL;
  2677.        s = s->next)
  2678.     {
  2679.       if (s->output_section == (asection *) NULL)
  2680.         {
  2681.           /* This section of the file is not attatched, root
  2682.              around for a sensible place for it to go */
  2683.  
  2684.           if (file->just_syms_flag)
  2685.         {
  2686.           /* We are only retrieving symbol values from this
  2687.                      file.  We want the symbols to act as though the
  2688.                      values in the file are absolute.  */
  2689.           s->output_section = bfd_abs_section_ptr;
  2690.           s->output_offset = s->vma;
  2691.         }
  2692.           else if (strcmp (s->name, "COMMON") == 0)
  2693.         {
  2694.           /* This is a lonely common section which must
  2695.              have come from an archive. We attatch to the
  2696.              section with the wildcard  */
  2697.           if (! link_info.relocateable
  2698.               && ! command_line.force_common_definition)
  2699.             {
  2700.               if (default_common_section == NULL)
  2701.             {
  2702. #if 0
  2703.               /* This message happens when using the
  2704.                              svr3.ifile linker script, so I have
  2705.                              disabled it.  */
  2706.               info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
  2707. #endif
  2708.               default_common_section =
  2709.                 lang_output_section_statement_lookup (".bss");
  2710.  
  2711.             }
  2712.               wild_doit (&default_common_section->children, s,
  2713.                  default_common_section, file);
  2714.             }
  2715.         }
  2716.           else if (ldemul_place_orphan (file, s))
  2717.         ;
  2718.           else
  2719.         {
  2720.           lang_output_section_statement_type *os =
  2721.           lang_output_section_statement_lookup (s->name);
  2722.  
  2723.           wild_doit (&os->children, s, os, file);
  2724.         }
  2725.         }
  2726.     }
  2727.     }
  2728. }
  2729.  
  2730.  
  2731. void
  2732. lang_set_flags (ptr, flags)
  2733.      int *ptr;
  2734.      CONST char *flags;
  2735. {
  2736.   boolean state = false;
  2737.  
  2738.   *ptr = 0;
  2739.   while (*flags)
  2740.     {
  2741.       if (*flags == '!')
  2742.     {
  2743.       state = false;
  2744.       flags++;
  2745.     }
  2746.       else
  2747.     state = true;
  2748.       switch (*flags)
  2749.     {
  2750.     case 'R':
  2751.       /*      ptr->flag_read = state; */
  2752.       break;
  2753.     case 'W':
  2754.       /*      ptr->flag_write = state; */
  2755.       break;
  2756.     case 'X':
  2757.       /*      ptr->flag_executable= state;*/
  2758.       break;
  2759.     case 'L':
  2760.     case 'I':
  2761.       /*      ptr->flag_loadable= state;*/
  2762.       break;
  2763.     default:
  2764.       einfo ("%P%F: invalid syntax in flags\n");
  2765.       break;
  2766.     }
  2767.       flags++;
  2768.     }
  2769. }
  2770.  
  2771. /* Call a function on each input file.  This function will be called
  2772.    on an archive, but not on the elements.  */
  2773.  
  2774. void
  2775. lang_for_each_input_file (func)
  2776.      void (*func) PARAMS ((lang_input_statement_type *));
  2777. {
  2778.   lang_input_statement_type *f;
  2779.  
  2780.   for (f = (lang_input_statement_type *) input_file_chain.head;
  2781.        f != NULL;
  2782.        f = (lang_input_statement_type *) f->next_real_file)
  2783.     func (f);
  2784. }
  2785.  
  2786. /* Call a function on each file.  The function will be called on all
  2787.    the elements of an archive which are included in the link, but will
  2788.    not be called on the archive file itself.  */
  2789.  
  2790. void
  2791. lang_for_each_file (func)
  2792.      void (*func) PARAMS ((lang_input_statement_type *));
  2793. {
  2794.   lang_input_statement_type *f;
  2795.  
  2796.   for (f = (lang_input_statement_type *) file_chain.head;
  2797.        f != (lang_input_statement_type *) NULL;
  2798.        f = (lang_input_statement_type *) f->next)
  2799.     {
  2800.       func (f);
  2801.     }
  2802. }
  2803.  
  2804. #if 0
  2805.  
  2806. /* Not used.  */
  2807.  
  2808. void
  2809. lang_for_each_input_section (func)
  2810.      void (*func) PARAMS ((bfd * ab, asection * as));
  2811. {
  2812.   lang_input_statement_type *f;
  2813.  
  2814.   for (f = (lang_input_statement_type *) file_chain.head;
  2815.        f != (lang_input_statement_type *) NULL;
  2816.        f = (lang_input_statement_type *) f->next)
  2817.     {
  2818.       asection *s;
  2819.  
  2820.       for (s = f->the_bfd->sections;
  2821.        s != (asection *) NULL;
  2822.        s = s->next)
  2823.     {
  2824.       func (f->the_bfd, s);
  2825.     }
  2826.     }
  2827. }
  2828.  
  2829. #endif
  2830.  
  2831. void
  2832. ldlang_add_file (entry)
  2833.      lang_input_statement_type * entry;
  2834. {
  2835.   bfd **pp;
  2836.  
  2837.   lang_statement_append (&file_chain,
  2838.              (lang_statement_union_type *) entry,
  2839.              &entry->next);
  2840.  
  2841.   /* The BFD linker needs to have a list of all input BFDs involved in
  2842.      a link.  */
  2843.   ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
  2844.   ASSERT (entry->the_bfd != output_bfd);
  2845.   for (pp = &link_info.input_bfds;
  2846.        *pp != (bfd *) NULL;
  2847.        pp = &(*pp)->link_next)
  2848.     ;
  2849.   *pp = entry->the_bfd;
  2850.   entry->the_bfd->usrdata = (PTR) entry;
  2851.   bfd_set_gp_size (entry->the_bfd, g_switch_value);
  2852.  
  2853.   /* Look through the sections and check for any which should not be
  2854.      included in the link.  We need to do this now, so that we can
  2855.      notice when the backend linker tries to report multiple
  2856.      definition errors for symbols which are in sections we aren't
  2857.      going to link.  FIXME: It might be better to entirely ignore
  2858.      symbols which are defined in sections which are going to be
  2859.      discarded.  This would require modifying the backend linker for
  2860.      each backend which might set the SEC_LINK_ONCE flag.  If we do
  2861.      this, we should probably handle SEC_EXCLUDE in the same way.  */
  2862.  
  2863.   bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) NULL);
  2864. }
  2865.  
  2866. void
  2867. lang_add_output (name, from_script)
  2868.      CONST char *name;
  2869.      int from_script;
  2870. {
  2871.   /* Make -o on command line override OUTPUT in script.  */
  2872.   if (had_output_filename == false || !from_script)
  2873.     {
  2874.       output_filename = name;
  2875.       had_output_filename = true;
  2876.     }
  2877. }
  2878.  
  2879.  
  2880. static lang_output_section_statement_type *current_section;
  2881.  
  2882. static int
  2883. topower (x)
  2884.      int x;
  2885. {
  2886.   unsigned int i = 1;
  2887.   int l;
  2888.  
  2889.   if (x < 0)
  2890.     return -1;
  2891.  
  2892.   for (l = 0; l < 32; l++) 
  2893.     {
  2894.       if (i >= (unsigned int) x)
  2895.     return l;
  2896.       i <<= 1;
  2897.     }
  2898.  
  2899.   return 0;
  2900. }
  2901.  
  2902. void
  2903. lang_enter_output_section_statement (output_section_statement_name,
  2904.                      address_exp, sectype, block_value,
  2905.                      align, subalign, ebase)
  2906.      const char *output_section_statement_name;
  2907.      etree_type * address_exp;
  2908.      enum section_type sectype;
  2909.      bfd_vma block_value;
  2910.      etree_type *align;
  2911.      etree_type *subalign;
  2912.      etree_type *ebase;
  2913. {
  2914.   lang_output_section_statement_type *os;
  2915.  
  2916.   current_section =
  2917.    os =
  2918.     lang_output_section_statement_lookup (output_section_statement_name);
  2919.  
  2920.  
  2921.  
  2922.   /* Add this statement to tree */
  2923.   /*  add_statement(lang_output_section_statement_enum,
  2924.       output_section_statement);*/
  2925.   /* Make next things chain into subchain of this */
  2926.  
  2927.   if (os->addr_tree ==
  2928.       (etree_type *) NULL)
  2929.   {
  2930.     os->addr_tree =
  2931.      address_exp;
  2932.   }
  2933.   os->sectype = sectype;
  2934.   if (sectype != noload_section)
  2935.     os->flags = SEC_NO_FLAGS;
  2936.   else
  2937.     os->flags = SEC_NEVER_LOAD;
  2938.   os->block_value = block_value ? block_value : 1;
  2939.   stat_ptr = &os->children;
  2940.  
  2941.   os->subsection_alignment = topower(
  2942.    exp_get_value_int(subalign, -1,
  2943.              "subsection alignment",
  2944.              0));
  2945.   os->section_alignment = topower(
  2946.    exp_get_value_int(align, -1,
  2947.              "section alignment", 0));
  2948.  
  2949.   os->load_base = ebase;
  2950. }
  2951.  
  2952.  
  2953. void
  2954. lang_final ()
  2955. {
  2956.   lang_output_statement_type *new =
  2957.     new_stat (lang_output_statement, stat_ptr);
  2958.  
  2959.   new->name = output_filename;
  2960. }
  2961.  
  2962. /* Reset the current counters in the regions */
  2963. static void
  2964. reset_memory_regions ()
  2965. {
  2966.   lang_memory_region_type *p = lang_memory_region_list;
  2967.  
  2968.   for (p = lang_memory_region_list;
  2969.        p != (lang_memory_region_type *) NULL;
  2970.        p = p->next)
  2971.     {
  2972.       p->old_length = (bfd_size_type) (p->current - p->origin);
  2973.       p->current = p->origin;
  2974.     }
  2975. }
  2976.  
  2977. void
  2978. lang_process ()
  2979. {
  2980.   lang_reasonable_defaults ();
  2981.   current_target = default_target;
  2982.  
  2983.   lang_for_each_statement (ldlang_open_output);    /* Open the output file */
  2984.  
  2985.   ldemul_create_output_section_statements ();
  2986.  
  2987.   /* Add to the hash table all undefineds on the command line */
  2988.   lang_place_undefineds ();
  2989.  
  2990.   /* Create a bfd for each input file */
  2991.   current_target = default_target;
  2992.   open_input_bfds (statement_list.head, false);
  2993.  
  2994.   ldemul_after_open ();
  2995.  
  2996.   /* Build all sets based on the information gathered from the input
  2997.      files.  */
  2998.   ldctor_build_sets ();
  2999.  
  3000.   /* Size up the common data */
  3001.   lang_common ();
  3002.  
  3003.   /* Run through the contours of the script and attatch input sections
  3004.      to the correct output sections
  3005.      */
  3006.   map_input_to_output_sections (statement_list.head, (char *) NULL,
  3007.                 (lang_output_section_statement_type *) NULL);
  3008.  
  3009.  
  3010.   /* Find any sections not attatched explicitly and handle them */
  3011.   lang_place_orphans ();
  3012.  
  3013.   ldemul_before_allocation ();
  3014.  
  3015.   /* We must record the program headers before we try to fix the
  3016.      section positions, since they will affect SIZEOF_HEADERS.  */
  3017.   lang_record_phdrs ();
  3018.  
  3019.   /* Now run around and relax if we can */
  3020.   if (command_line.relax)
  3021.     {
  3022.       /* First time round is a trial run to get the 'worst case'
  3023.      addresses of the objects if there was no relaxing.  */
  3024.       lang_size_sections (statement_list.head,
  3025.               abs_output_section,
  3026.               &(statement_list.head), 0, (bfd_vma) 0, false);
  3027.  
  3028.       /* Keep relaxing until bfd_relax_section gives up.  */
  3029.       do
  3030.     {
  3031.       reset_memory_regions ();
  3032.  
  3033.       relax_again = false;
  3034.  
  3035.       /* Do all the assignments with our current guesses as to
  3036.          section sizes.  */
  3037.       lang_do_assignments (statement_list.head,
  3038.                    abs_output_section,
  3039.                    (fill_type) 0, (bfd_vma) 0);
  3040.  
  3041.       /* Perform another relax pass - this time we know where the
  3042.          globals are, so can make better guess.  */
  3043.       lang_size_sections (statement_list.head,
  3044.                   abs_output_section,
  3045.                   &(statement_list.head), 0, (bfd_vma) 0, true);
  3046.     }
  3047.       while (relax_again);
  3048.     }
  3049.   else
  3050.     {
  3051.       /* Size up the sections.  */
  3052.       lang_size_sections (statement_list.head,
  3053.               abs_output_section,
  3054.               &(statement_list.head), 0, (bfd_vma) 0, false);
  3055.     }
  3056.  
  3057.   /* See if anything special should be done now we know how big
  3058.      everything is.  */
  3059.   ldemul_after_allocation ();
  3060.  
  3061.   /* Fix any .startof. or .sizeof. symbols.  */
  3062.   lang_set_startof ();
  3063.  
  3064.   /* Do all the assignments, now that we know the final restingplaces
  3065.      of all the symbols */
  3066.  
  3067.   lang_do_assignments (statement_list.head,
  3068.                abs_output_section,
  3069.                (fill_type) 0, (bfd_vma) 0);
  3070.  
  3071.   /* Make sure that we're not mixing architectures */
  3072.  
  3073.   lang_check ();
  3074.  
  3075.   /* Final stuffs */
  3076.  
  3077.   ldemul_finish ();
  3078.   lang_finish ();
  3079. }
  3080.  
  3081. /* EXPORTED TO YACC */
  3082.  
  3083. void
  3084. lang_add_wild (section_name, filename)
  3085.      CONST char *CONST section_name;
  3086.      CONST char *CONST filename;
  3087. {
  3088.   lang_wild_statement_type *new = new_stat (lang_wild_statement,
  3089.                         stat_ptr);
  3090.  
  3091.   if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
  3092.     {
  3093.       placed_commons = true;
  3094.     }
  3095.   if (filename != (char *) NULL)
  3096.     {
  3097.       lang_has_input_file = true;
  3098.     }
  3099.   new->section_name = section_name;
  3100.   new->filename = filename;
  3101.   lang_list_init (&new->children);
  3102. }
  3103.  
  3104. void
  3105. lang_section_start (name, address)
  3106.      CONST char *name;
  3107.      etree_type * address;
  3108. {
  3109.   lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
  3110.  
  3111.   ad->section_name = name;
  3112.   ad->address = address;
  3113. }
  3114.  
  3115. /* Set the start symbol to NAME.  CMDLINE is nonzero if this is called
  3116.    because of a -e argument on the command line, or zero if this is
  3117.    called by ENTRY in a linker script.  Command line arguments take
  3118.    precedence.  */
  3119.  
  3120. /* WINDOWS_NT.  When an entry point has been specified, we will also force
  3121.    this symbol to be defined by calling ldlang_add_undef (equivalent to 
  3122.    having switch -u entry_name on the command line).  The reason we do
  3123.    this is so that the user doesn't have to because they would have to use
  3124.    the -u switch if they were specifying an entry point other than 
  3125.    _mainCRTStartup.  Specifically, if creating a windows application, entry
  3126.    point _WinMainCRTStartup must be specified.
  3127.      What I have found for non console applications (entry not _mainCRTStartup)
  3128.    is that the .obj that contains mainCRTStartup is brought in since it is
  3129.    the first encountered in libc.lib and it has other symbols in it which will
  3130.    be pulled in by the link process.  To avoid this, adding -u with the entry
  3131.    point name specified forces the correct .obj to be used.  We can avoid
  3132.    making the user do this by always adding the entry point name as an
  3133.    undefined symbol.  */
  3134.  
  3135. void
  3136. lang_add_entry (name, cmdline)
  3137.      CONST char *name;
  3138.      boolean cmdline;
  3139. {
  3140.   if (entry_symbol == NULL
  3141.       || cmdline
  3142.       || ! entry_from_cmdline)
  3143.     {
  3144.       entry_symbol = name;
  3145.       entry_from_cmdline = cmdline;
  3146.     }
  3147. #if 0 
  3148.   /* don't do this yet.  It seems to work (the executables run), but the 
  3149.      image created is very different from what I was getting before indicating
  3150.      that something else is being pulled in.  When everything else is working,
  3151.      then try to put this back in to see if it will do the right thing for
  3152.      other more complicated applications */
  3153.   ldlang_add_undef (name);
  3154. #endif
  3155. }
  3156.  
  3157. void
  3158. lang_add_target (name)
  3159.      CONST char *name;
  3160. {
  3161.   lang_target_statement_type *new = new_stat (lang_target_statement,
  3162.                           stat_ptr);
  3163.  
  3164.   new->target = name;
  3165.  
  3166. }
  3167.  
  3168. void
  3169. lang_add_map (name)
  3170.      CONST char *name;
  3171. {
  3172.   while (*name)
  3173.     {
  3174.       switch (*name)
  3175.     {
  3176.       case 'F':
  3177.       map_option_f = true;
  3178.       break;
  3179.     }
  3180.       name++;
  3181.     }
  3182. }
  3183.  
  3184. void
  3185. lang_add_fill (exp)
  3186.      int exp;
  3187. {
  3188.   lang_fill_statement_type *new = new_stat (lang_fill_statement,
  3189.                         stat_ptr);
  3190.  
  3191.   new->fill = exp;
  3192. }
  3193.  
  3194. void
  3195. lang_add_data (type, exp)
  3196.      int type;
  3197.      union etree_union *exp;
  3198. {
  3199.  
  3200.   lang_data_statement_type *new = new_stat (lang_data_statement,
  3201.                         stat_ptr);
  3202.  
  3203.   new->exp = exp;
  3204.   new->type = type;
  3205.  
  3206. }
  3207.  
  3208. /* Create a new reloc statement.  RELOC is the BFD relocation type to
  3209.    generate.  HOWTO is the corresponding howto structure (we could
  3210.    look this up, but the caller has already done so).  SECTION is the
  3211.    section to generate a reloc against, or NAME is the name of the
  3212.    symbol to generate a reloc against.  Exactly one of SECTION and
  3213.    NAME must be NULL.  ADDEND is an expression for the addend.  */
  3214.  
  3215. void
  3216. lang_add_reloc (reloc, howto, section, name, addend)
  3217.      bfd_reloc_code_real_type reloc;
  3218.      reloc_howto_type *howto;
  3219.      asection *section;
  3220.      const char *name;
  3221.      union etree_union *addend;
  3222. {
  3223.   lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
  3224.   
  3225.   p->reloc = reloc;
  3226.   p->howto = howto;
  3227.   p->section = section;
  3228.   p->name = name;
  3229.   p->addend_exp = addend;
  3230.  
  3231.   p->addend_value = 0;
  3232.   p->output_section = NULL;
  3233.   p->output_vma = 0;
  3234. }
  3235.  
  3236. void
  3237. lang_add_assignment (exp)
  3238.      etree_type * exp;
  3239. {
  3240.   lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
  3241.                           stat_ptr);
  3242.  
  3243.   new->exp = exp;
  3244. }
  3245.  
  3246. void
  3247. lang_add_attribute (attribute)
  3248.      enum statement_enum attribute;
  3249. {
  3250.   new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
  3251. }
  3252.  
  3253. void
  3254. lang_startup (name)
  3255.      CONST char *name;
  3256. {
  3257.   if (startup_file != (char *) NULL)
  3258.     {
  3259.       einfo ("%P%Fmultiple STARTUP files\n");
  3260.     }
  3261.   first_file->filename = name;
  3262.   first_file->local_sym_name = name;
  3263.   first_file->real = true;
  3264.  
  3265.   startup_file = name;
  3266. }
  3267.  
  3268. void
  3269. lang_float (maybe)
  3270.      boolean maybe;
  3271. {
  3272.   lang_float_flag = maybe;
  3273. }
  3274.  
  3275. void
  3276. lang_leave_output_section_statement (fill, memspec)
  3277.      bfd_vma fill;
  3278.      CONST char *memspec;
  3279. {
  3280.   current_section->fill = fill;
  3281.   current_section->region = lang_memory_region_lookup (memspec);
  3282.   stat_ptr = &statement_list;
  3283. }
  3284.  
  3285. /*
  3286.  Create an absolute symbol with the given name with the value of the
  3287.  address of first byte of the section named.
  3288.  
  3289.  If the symbol already exists, then do nothing.
  3290. */
  3291. void
  3292. lang_abs_symbol_at_beginning_of (secname, name)
  3293.      const char *secname;
  3294.      const char *name;
  3295. {
  3296.   struct bfd_link_hash_entry *h;
  3297.  
  3298.   h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
  3299.   if (h == (struct bfd_link_hash_entry *) NULL)
  3300.     einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
  3301.  
  3302.   if (h->type == bfd_link_hash_new
  3303.       || h->type == bfd_link_hash_undefined)
  3304.     {
  3305.       asection *sec;
  3306.  
  3307.       h->type = bfd_link_hash_defined;
  3308.  
  3309.       sec = bfd_get_section_by_name (output_bfd, secname);
  3310.       if (sec == (asection *) NULL)
  3311.     h->u.def.value = 0;
  3312.       else
  3313.     h->u.def.value = bfd_get_section_vma (output_bfd, sec);
  3314.  
  3315.       h->u.def.section = bfd_abs_section_ptr;
  3316.     }
  3317. }
  3318.  
  3319. /*
  3320.  Create an absolute symbol with the given name with the value of the
  3321.  address of the first byte after the end of the section named.
  3322.  
  3323.  If the symbol already exists, then do nothing.
  3324. */
  3325. void
  3326. lang_abs_symbol_at_end_of (secname, name)
  3327.      const char *secname;
  3328.      const char *name;
  3329. {
  3330.   struct bfd_link_hash_entry *h;
  3331.  
  3332.   h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
  3333.   if (h == (struct bfd_link_hash_entry *) NULL)
  3334.     einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
  3335.  
  3336.   if (h->type == bfd_link_hash_new
  3337.       || h->type == bfd_link_hash_undefined)
  3338.     {
  3339.       asection *sec;
  3340.  
  3341.       h->type = bfd_link_hash_defined;
  3342.  
  3343.       sec = bfd_get_section_by_name (output_bfd, secname);
  3344.       if (sec == (asection *) NULL)
  3345.     h->u.def.value = 0;
  3346.       else
  3347.     h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
  3348.               + bfd_section_size (output_bfd, sec));
  3349.  
  3350.       h->u.def.section = bfd_abs_section_ptr;
  3351.     }
  3352. }
  3353.  
  3354. void
  3355. lang_statement_append (list, element, field)
  3356.      lang_statement_list_type * list;
  3357.      lang_statement_union_type * element;
  3358.      lang_statement_union_type ** field;
  3359. {
  3360.   *(list->tail) = element;
  3361.   list->tail = field;
  3362. }
  3363.  
  3364. /* Set the output format type.  -oformat overrides scripts.  */
  3365.  
  3366. void
  3367. lang_add_output_format (format, big, little, from_script)
  3368.      const char *format;
  3369.      const char *big;
  3370.      const char *little;
  3371.      int from_script;
  3372. {
  3373.   if (output_target == NULL || !from_script)
  3374.     {
  3375.       if (command_line.endian == ENDIAN_BIG
  3376.       && big != NULL)
  3377.     format = big;
  3378.       else if (command_line.endian == ENDIAN_LITTLE
  3379.            && little != NULL)
  3380.     format = little;
  3381.  
  3382.       output_target = format;
  3383.     }
  3384. }
  3385.  
  3386. /* Enter a group.  This creates a new lang_group_statement, and sets
  3387.    stat_ptr to build new statements within the group.  */
  3388.  
  3389. void
  3390. lang_enter_group ()
  3391. {
  3392.   lang_group_statement_type *g;
  3393.  
  3394.   g = new_stat (lang_group_statement, stat_ptr);
  3395.   lang_list_init (&g->children);
  3396.   stat_ptr = &g->children;
  3397. }
  3398.  
  3399. /* Leave a group.  This just resets stat_ptr to start writing to the
  3400.    regular list of statements again.  Note that this will not work if
  3401.    groups can occur inside anything else which can adjust stat_ptr,
  3402.    but currently they can't.  */
  3403.  
  3404. void
  3405. lang_leave_group ()
  3406. {
  3407.   stat_ptr = &statement_list;
  3408. }
  3409.  
  3410. /* Add a new program header.  This is called for each entry in a PHDRS
  3411.    command in a linker script.  */
  3412.  
  3413. void
  3414. lang_new_phdr (name, type, filehdr, phdrs, at, flags)
  3415.      const char *name;
  3416.      etree_type *type;
  3417.      boolean filehdr;
  3418.      boolean phdrs;
  3419.      etree_type *at;
  3420.      etree_type *flags;
  3421. {
  3422.   struct lang_phdr *n, **pp;
  3423.  
  3424.   n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
  3425.   n->next = NULL;
  3426.   n->name = name;
  3427.   n->type = exp_get_value_int (type, 0, "program header type",
  3428.                    lang_final_phase_enum);
  3429.   n->filehdr = filehdr;
  3430.   n->phdrs = phdrs;
  3431.   n->at = at;
  3432.   n->flags = flags;
  3433.  
  3434.   for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
  3435.     ;
  3436.   *pp = n;
  3437. }
  3438.  
  3439. /* Record that a section should be placed in a phdr.  */
  3440.  
  3441. void
  3442. lang_section_in_phdr (name)
  3443.      const char *name;
  3444. {
  3445.   struct lang_output_section_phdr_list *n;
  3446.  
  3447.   n = ((struct lang_output_section_phdr_list *)
  3448.        stat_alloc (sizeof (struct lang_output_section_phdr_list)));
  3449.   n->name = name;
  3450.   n->used = false;
  3451.   n->next = current_section->phdrs;
  3452.   current_section->phdrs = n;
  3453. }
  3454.  
  3455. /* Record the program header information in the output BFD.  FIXME: We
  3456.    should not be calling an ELF specific function here.  */
  3457.  
  3458. static void
  3459. lang_record_phdrs ()
  3460. {
  3461.   unsigned int alc;
  3462.   asection **secs;
  3463.   struct lang_output_section_phdr_list *last;
  3464.   struct lang_phdr *l;
  3465.   lang_statement_union_type *u;
  3466.  
  3467.   alc = 10;
  3468.   secs = xmalloc (alc * sizeof (asection *));
  3469.   last = NULL;
  3470.   for (l = lang_phdr_list; l != NULL; l = l->next)
  3471.     {
  3472.       unsigned int c;
  3473.       flagword flags;
  3474.       bfd_vma at;
  3475.  
  3476.       c = 0;
  3477.       for (u = lang_output_section_statement.head;
  3478.        u != NULL;
  3479.        u = u->output_section_statement.next)
  3480.     {
  3481.       lang_output_section_statement_type *os;
  3482.       struct lang_output_section_phdr_list *pl;
  3483.  
  3484.       os = &u->output_section_statement;
  3485.  
  3486.       pl = os->phdrs;
  3487.       if (pl != NULL)
  3488.         last = pl;
  3489.       else
  3490.         {
  3491.           if (os->sectype == noload_section
  3492.           || os->bfd_section == NULL
  3493.           || (os->bfd_section->flags & SEC_ALLOC) == 0)
  3494.         continue;
  3495.           pl = last;
  3496.         }
  3497.  
  3498.       if (os->bfd_section == NULL)
  3499.         continue;
  3500.  
  3501.       for (; pl != NULL; pl = pl->next)
  3502.         {
  3503.           if (strcmp (pl->name, l->name) == 0)
  3504.         {
  3505.           if (c >= alc)
  3506.             {
  3507.               alc *= 2;
  3508.               secs = xrealloc (secs, alc * sizeof (asection *));
  3509.             }
  3510.           secs[c] = os->bfd_section;
  3511.           ++c;
  3512.           pl->used = true;
  3513.         }
  3514.         }
  3515.     }
  3516.  
  3517.       if (l->flags == NULL)
  3518.     flags = 0;
  3519.       else
  3520.     flags = exp_get_vma (l->flags, 0, "phdr flags",
  3521.                  lang_final_phase_enum);
  3522.  
  3523.       if (l->at == NULL)
  3524.     at = 0;
  3525.       else
  3526.     at = exp_get_vma (l->at, 0, "phdr load address",
  3527.               lang_final_phase_enum);
  3528.  
  3529.       if (! bfd_record_phdr (output_bfd, l->type,
  3530.                  l->flags == NULL ? false : true,
  3531.                  flags,
  3532.                  l->at == NULL ? false : true,
  3533.                  at, l->filehdr, l->phdrs, c, secs))
  3534.     einfo ("%F%P: bfd_record_phdr failed: %E\n");
  3535.     }
  3536.  
  3537.   free (secs);
  3538.  
  3539.   /* Make sure all the phdr assignments succeeded.  */
  3540.   for (u = lang_output_section_statement.head;
  3541.        u != NULL;
  3542.        u = u->output_section_statement.next)
  3543.     {
  3544.       struct lang_output_section_phdr_list *pl;
  3545.  
  3546.       if (u->output_section_statement.bfd_section == NULL)
  3547.     continue;
  3548.  
  3549.       for (pl = u->output_section_statement.phdrs;
  3550.        pl != NULL;
  3551.        pl = pl->next)
  3552.     if (! pl->used && strcmp (pl->name, "NONE") != 0)
  3553.       einfo ("%X%P: section `%s' assigned to non-existent phdr `%s'\n",
  3554.          u->output_section_statement.name, pl->name);
  3555.     }
  3556. }
  3557.